PowerShell is a scripting language that can help you automate computer tasks simply and reliably. Find out how to use it to start building scripts for your Windows PC.

PowerShell Scripting Overview

To start making simple PowerShell scripts on Windows, you’ll need to know about three things:

Combining all three, you can make simple scripts to help automate tasks.

Potato chip script

Here’s an example pipeline showing how you may combine several Cmdlets to achieve a task:

To run this pipeline in PowerShell, you would use something like this command:

Get process Cmdlet

Cmdlets in Detail

Like any scripting language, PowerShell comes pre-built with commands to manipulate objects and perform various tasks. These fundamental commands within PowerShell are known as Cmdlets.

Cmdlets (pronounced as command-lets) are small, single-function commands used in the PowerShell environment using the .dll extension. They are lightweight pieces of code that execute quicker than functions due to their compiled nature.

Using Get-Process with parameter

There are thousands of Cmdlets available on PowerShell. You do not need to learn all of them. You can start by learninga few basic PowerShell Cmdletsthen keep learning as you go through your scripting journey. To view the Cmdlets that are already installed on your computer, run:

If you scan through the results, you’ll notice that they follow a verb-noun naming convention. This makes it easier to understand what a certain Cmdlet does.

Cmdlet syntax parameters

For example, Get-Command gives a list of commands available in PowerShell. Get-Process gets you the processes that are currently active, and Copy-Item copies one or more files to a specific directory.

Cmdlets also come bundled with extensive documentation that includes examples, usage instructions, and explanations of their functionality.

Using Id Parameter on Cmdlet

To find out more about a specific Cmdlet and how to use it, run:

PowerShell Parameters Explained

Cmdlets can accept parameters to vary their behavior. When you run a Cmdlet or function, you can provide parameter values to specify what, when, where, and how each PowerShell command runs.

For example, Get-Process will get and list of all the active processes within your operating system:

But what if you only want to get a specific process? You can do so using parameters. For example, to get all Slack processes, you can use theNameparameter with the Get-Process Cmdlet:

You will then see only those processes with the name “slack”:

Some parameters are “positional” which means their name is optional. In this case,Get-Process -Name SlackandGet-Process Slackdo the same thing.

Each Cmdlet will accept different types of parameters. Use the Get-Help command to view the accepted parameters of a Cmdlet in the SYNTAX section.

You’ll see a list of all possible ways you can run the given Cmdlet:

In this case, the Get-Process Cmdlet accepts parameters such asName,Id,ComputerName,Module,FileVersionInfo, and other common parameters. The symbols here signify:

Parameter doesn’t accept input

Indicates parameter name

Angled Brackets

Placeholder for text

Parameter that can accept one or more values

Parameter accepts a set of values

Parameters that accept inputs will indicate the type of data they require, such as a string, integer, boolean, or DateTime. For example, this:

Means that the Name parameter accepts one or more string values, while this:

Means that the Id parameter accepts one or more integer values.

The earlier Get-Process example used the Name parameter to narrow down the results. However, if you want to narrow it down to an even more specific process, you can use the ID parameter, which requires an integer as stated in its syntax.

You should then see just one process in the list:

Creating a Pipeline

PowerShell treats all data as objects. To build a script, these objects flow through a series of Cmdlets or functions connected by a pipe symbol ( | ). Choosing the right Cmdlets and connecting them in a logical sequence using the pipeline is crucial for an effective script.

Say you’re creating a script to sort and display the five files taking the most storage space in a folder. There aremore robust ways of creating a file-sorting script, but the following one-liner is easy to understand:

To do this in PowerShell, use a pipeline that looks something like this:

Saving Pipeline as a PS1 Script

Now that we have a working pipeline, you can save it as a PS1 script file, so you don’t have to keep typing it every time you use it.

The simplest way to create a PS1 file is to paste your script into Notepad and save the file with the .ps1 extension.

Once you’ve created your PS1 file, you may use it in PowerShell by running./ScriptName.ps1:

Ifyou get a permission error, the quickest workaround is to run PowerShell as administrator when running your scripts.

Congratulations! You can now create PowerShell PS1 scripts.

PowerShell Is Also Available on Linux and macOS

PowerShell is one of the best beginner scripting languages anyone can learn. Although PowerShell was only limited to Windows in the past, the updated version of PowerShell is now also available on macOS and many Linux distributions! This makes PowerShell even more appealing as you can transfer your knowledge from Windows to other operating systems.