PowerShell

Miscellaneous tips to build useful one-liners or PowerShell scripts.


Most Useful Cmdlets

PowerShell Cmdlet PowerShell Alias cmd.exe Linux
Get-ChildItem ls, dir, gci dir ls
Copy-Item cp, copy, cpi copy cp
Move-Item mv, move, mi move mv
Select-String sls find, findstr grep
Get-Help man, help help man
Get-Content cat, type, gc type cat
Get-Process ps, gps tasklist ps
Get-Location pwd, gl cd pwd

Download a file using HTTP:

(New-Object System.Net.WebClient).DownloadFile("[URL]","[output_file]")

Environment Variables

A few useful environment variables:

Variable Description
$host Displays PowerShell version
$null /dev/null equivalent

List of all environment variables:

ls env:

Display the value of environment variable:

echo $env:[variable]

Example: echo $env:PROCESSOR_ARCHITECTURE

List of all variables:

ls variable:

Help

A few useful way to get help on how to use PowerShell.

Find cmdlets: Get-Command, gcm

Get-Command [query]

Use * in [query] to search for specific cmdlets. Example: gcm *process*

Get help about a cmdlet: Get-Help, help, man

Get-Help [cmdlet]
Get-Help [cmdlet] -detailed
Get-Help [cmdlet] -examples
Get-Help [cmdlet] -full
Get-Help [cmdlet] -online

Use Update-Help to download the latest version of help from Microsoft.

Filter Output

Use Where-Object, alias ? to filter down a group of objects and select only the ones that we want based on their properties.

[cmdlet] | ? { $_.[property] [-operator] [query] }
Operator Description
-eq Equals (case-insensitive)
-ceq Equals (case-sensitive)
-ne Not-equals (case-insensitive)
-cne Not-equals (case-sensitive)
-like Substrings (with * for a wildcard)
-gt Greater than
-lt Lesser than

Example: Get-Service | ? { $_.status -eq "running" }

Tip

In order to count the number of results (like wc -l on Linux):

([cmdlet]).count

Strip-down Output

Use Select-Object, alias select, to create new objects from those passed down the pipeline with a subset of their properties.

[cmdlet] | Select-Object -Property [p1], [p2], [...]

Example: ps | Select-Object -Property Name, Id

Format Output

As each cmdlet outputs an object, you can fine-tune its output by displaying only the properties you’re interested in.

Get the list of properties and method of an object output: Get-Member, gm

[cmdlet] | Get-Member

Format the output: Format-List, Format-Table

[cmdlet] | Format-List -Property [p1], [p2], ...
[cmdlet] | Format-Table -Property [p1], [p2], ...

Tip

Pipe any cmdlet to Out-Host -Paging to enable you to page through a long output (like less).

Example: ps | Out-Host -Paging

Intract with Output

Interact with all objects of a cmdlet output with ForEach-Object cmdlet, alias %. It takes each object piped to it and then run a command of your choice. Refer to the current object with $_.

[cmdlet] | % { $_ }

Example: ps | % { stop-process $_ }

Simple Loop

Build a simple for loop using this feature by piping list of integers to %.

[start]..[stop] | % { $_ }

Example: 0..9 | % { echo $_ }

Base64

Decode base64-encoded string:

[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("[BASE64]"))

Encode a string to base64:

[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("[STRING]"))

Encode a file to base64:

[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes([System.IO.File]::ReadAllText("[\path\to\input\file.txt]")))

Base64 (unicode)

Decode base64-encoded unicode-string:

[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("[BASE64-UNICODE]"))

Encode a string to unicode-base64:s

[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("[STRING]"))

Encode a file to unicode-base64:

[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes([System.IO.File]::ReadAllText("[\path\to\input\file.txt]")))