How to test PowerShell code with Pester 25 basic PowerShell commands for Windows administrators
X

How to create and run Windows PowerShell scripts

Once administrators get comfortable with PowerShell basics, it's time to start scripting. This PowerShell scripting tutorial for beginners helps you efficiently complete tasks.

Editor's note: Adam Bertram originally wrote this article, and Reda Chouffani has expanded it.

Even though administrators can accomplish many tasks with ad hoc PowerShell commands, learning how to script brings your productivity to the next level.

Most administrators get introduced to PowerShell by working with its cmdlets from the command line. Once you're familiar with executing commands and learn how to work with objects and the PowerShell pipeline, then it's time to stitch these commands together into a script.

Going from entering commands to scripting isn't difficult if you think about a script as a long-form command. Scripts execute one or more PowerShell commands together to accomplish a single task.

What are the basics of PowerShell scripting?

The purpose of PowerShell is to provide a command-line shell and scripting capabilities to automate and configure Microsoft's platforms. This helps IT professionals with many of their administrative tasks.

The platform provides several capabilities that some of the administrative portals don't offer. Scripting is also available to administrators to automate some of their administrative activities.

PowerShell scripting starts with opening some of the PowerShell terminals on the administrator's machine. This can be done from a Windows machine or a Linux machine as well. Once the administrator launches the PowerShell tool, they can enter any of the desired commands they need.

The syntax used is specific to PowerShell and provides a way for administrators to interact and confirm or extract information from different systems based on the modules used. PowerShell also offers admins the ability to script their tasks to automate any repeatable processes or tasks they perform on the different systems.

This PowerShell scripting tutorial for beginners helps you make the transition from basic use to advanced functions to help with tasks like server deployments or assigning licenses to users.

How to write and create a PowerShell script

Let's start with an example of querying Windows services from Windows Server. The Get-Service cmdlet queries all the services running on a local Windows computer from the PowerShell console.

The Get-Service cmdlet
The Get-Service cmdlet lists all the services on a local Windows machine.

If you want to find all of the stopped services, try to start them and add some logging capabilities, you need to create a PowerShell script.

In the example below, the PowerShell script contains more than one line, a construct known as a loop, as well as containing references to multiple commands:

$services = Get-Service | Where-Object {$_.Status -eq 'Stopped'}
foreach ($service in $services) {
Add-Content -Path 'C:\ScriptLog.log' -value "Attempting to start
$($_.DisplayName)…"
$service | Start-Service
}

You can build the script in the PowerShell Integrated Scripting Environment (ISE) editor that comes with Windows. Open the PowerShell ISE editor, copy the code and save it as Start-StoppedServices.ps1. All PowerShell scripts have a PS1 extension for Windows that prompts the PowerShell engine to execute the script.

As another example, in Windows 10 and Windows 11 machines, you can use PowerShell to retrieve some of the Windows Update logs to determine if a recent workstation issue is the result of a recent Windows update. To perform this task, run the following command in PowerShell:

Get-WindowsUpdateLog
Get-WindowsUpdateLog command
A common PowerShell command to check Windows updates

Once the command completes its execution, a file is generated to provide all the details of all updates performed on the specific machine.

How to save a PowerShell script

Microsoft provides several ways to create PowerShell scripts, including the following:

  • PowerShell ISE.
  • Notepad.
  • Notepad++.
  • Visual Studio (Standard, Pro, Enterprise, Code).

Once the script is created, you can save it locally or to server storage using the PS1 extension. You can also use code repository platforms to ensure the versioning of your scripts.

How to execute a PowerShell Script

With your script saved in a single file, instead of typing out all that code, you can execute it as a single command by passing the entire script to PowerShell.

If you go back to the PowerShell console, you can run C:-StoppedServices.ps1 to execute all the code in that script. Creating scripts is similar to creating commands; it lets you piece together code and run it as a single unit.

You can also run PowerShell scripts from the cmd.exe command interpreter or from a batch file. You can launch a script from anywhere by invoking the PowerShell engine and passing a script to it.

In the following example, you use the File parameter to pass the script to PowerShell without opening the PowerShell console.

powershell -File C:\Start-StoppedServices.ps1

Best practices when designing PowerShell scripts

Once you build up a proficiency with PowerShell cmdlets and its scripting language, there are endless ways to solve problems with code. This scripting tutorial wouldn't be complete without a few best practices that can help you construct proper scripts.

Simplify things, and think of your script as something that performs a single task or that solves a single problem. In our example, the script starts all the services that have stopped. Administrators can benefit from writing basic scripts that complete repetitive tasks, providing them with more time to work on more demanding tasks.

In addition, design scripts that require little maintenance. If your script only performs one task, you can eventually combine it with other scripts to create building blocks. Make scripts modular so you don't have to change the code constantly. Parameters can help you further customize your scripts by defining variables to pass different values during script execution.

This should start you on your way from clicking through multiple menu options to complete a task to producing code that handles complex jobs in a flash.

Next Steps

Do-While vs. Do-Until in PowerShell: What's the difference?

Dig Deeper on IT operations and infrastructure management

Cloud Computing
Enterprise Desktop
Virtual Desktop
Close