PowerShell

PowerShell is a cross-platform command line shell by Microsoft that works in terms of objects rather than traditional text streams. It's a powerful automation tool that's particularly useful when working with Azure.

Note that whilst PowerShell Core is cross-platform Windows PowerShell is not. It's worth installing PowerShell Core on Windows and using this in place of Windows PowerShell to ensure you're getting the same experience as Linux and macOS users.

Installation

Follow Microsoft's instructions.

The basics

Whilst PowerShell can work as a traditional shell, running external commands and manipulating their input and output streams, it's most powerful when it's working with objects. Whereas traditional shells might encourage you to run a command then match lines in its text:

$ ps -eFly | grep init
S root         1     0  0  80   0   312  2234 ?        0 12:46 ?        00:00:00 /init
S root         6     1  0  80   0   224  2235 -        0 12:46 tty1     00:00:00 /init
S lcarrier   335     7  0  80   0  1196  3701 -        0 13:04 tty1     00:00:00 grep --color=auto init

PowerShell allows you to do the same thing using objects on its pipeline:

> Get-Process | Where-Object { $_.ProcessName -like 'Teams' }

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     23    24.70      63.53       0.11   11336   5 Teams
     36   171.57     129.45     279.06   22852   5 Teams
     27    35.53      56.09       0.83   23020   5 Teams
     74   108.05     133.43     288.91   28784   5 Teams
     58   135.89     141.19     732.64   31668   5 Teams
    149   553.26     461.09     615.31   33840   5 Teams

There are different types of PowerShell commands:

  • Cmdlets are commands native to the PowerShell environment; they're not accessible outside of the shell. You can identify these by their distinctive <Verb>-[Prefix]<Noun> names.
  • Aliases are named after their counterparts in traditional shells; they're intended to reduce your typing by creating an alias for a command (e.g. ft for Format-Table, g for git).
  • Functions are similar to cmdlets but are written in PowerShell and don't have a CmdletBinding attribute for their parameters.
    • Filters are functions that run on each object in a pipeline for filtering or processing.
  • Scripts
  • Applications can also be called. Whilst not necessary, it's usually best to be explicit and use the call operator (&) to denote calls to external programs.

These commands are usually bundled together into Modules which can be easily installed. These are usually distributed via the PowerShell Gallery.

To find commands you can use Get-Command:

  • Get-Command -Verb Approve will show you all commands that use the Approve verb.
  • Get-Command -Noun Process will show you all commands that can operate on processes.
  • Get-Command -Module Az will show you all commands defined by the Az module.

For more detailed help, use Get-Help:

  • Get-Help -Name Get-Process will get summary information.
  • Get-Help -Name Get-Process -Examples will yield some usage examples.
  • Get-Help -Name Get-Process -Full will give you all the offline documentation available.