What Is Windows PowerShell Scripting?

What Is Windows PowerShell Scripting?

  19 Jan 2024

Key Takeaways

  • PowerShell is a scripting language that can help automate computer tasks.
  • Cmdlets are single-function Powershell commands. You can view available commands with “Get-Command.”
  • PowerShell is available on Windows, macOS, and Linux.


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:

  • Cmdlets: single function commands.
  • Parameters: specify the what, when, where, and how of Cmdlets/commands.
  • Pipeline: takes the output of your Cmdlet and its parameters and connects it to other Cmdlets and functions.

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

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

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

 Get-Object Potato | Peel-Object | Chop-Object Chips| Cook-Object DeepFry `
  | Place-Object Bowl

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.

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

 Get-Command 

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.

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.

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

 Get-Help <name-of-cmdlet> 

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:

Get process Cmdlet

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 the Name parameter with the Get-Process Cmdlet:

 Get-Process -Name Slack 

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

Using Get-Process with parameter

Some parameters are “positional” which means their name is optional. In this case, Get-Process -Name Slack and Get-Process Slack do 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.

 Get-Help Get-Process 

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

Cmdlet syntax parameters

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

Symbol

Name

Meaning

Blank

Parameter doesn’t accept input

Hyphen

Indicates parameter name

<>

Angled Brackets

Placeholder for text

[]

Brackets

Parameter that can accept one or more values

{}

Braces

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:

 Get-Process [[-Name] <string[]>] 

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

 Get-Process -Id <int[]> 

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.

 Get-Process -Id 3016 

You should then see just one process in the list:

Using Id Parameter on Cmdlet

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 are more robust ways of creating a file-sorting script, but the following one-liner is easy to understand:

Sort-LargeFiles PS1 script

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

 Get-ChildItem -Path "C:Directory" -File | Sort-Object Length -Descending `
 | Select-Object -First 5 | Format-Table Name, Length -AutoSize

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.

Creating a script with notepad

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

Using PS1 script

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.

Leave a Reply

Your email address will not be published. Required fields are marked *