olly - Fotolia

Get started Bring yourself up to speed with our introductory content.

Start scripting with this PowerShell tutorial for beginners

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

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

Most administrators get introduced to PowerShell by working with its cmdlets from the command line. Once you are 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.

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

How to create a PowerShell script

Let's start our PowerShell tutorial for beginners 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.

Execute a PowerShell Script

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

Simplify things and think of your script as something that performs a single task or that solves a single problem.

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 allows you to 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 PowerShell tutorial for beginners 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 PowerShell tutorial for beginners 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.

This was last published in September 2018

Dig Deeper on Windows administration tools

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

What are some PowerShell scripting practices that other Windows administrators should know?
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchSQLServer

SearchEnterpriseDesktop

SearchVirtualDesktop

Close