Powershell Cheat Sheet

Useful Commands

Get-Help

Displays help about windows Powershell cmdlets and concepts

Get-Command

Retrieves a list of all available Powershell cmdlets

Get-ChildItem

Gets the files and folders in a file system drive

Get-Content

Gets the contents of a file

Get-Member

Gets the properties and methods of objects

Where-Object

Selects objects from a collection based on their property values

ForEach-Object

Performs an operation against each item in a collection of input objects

Select-Object

Selects objects or object properties

Select-String

Finds text in strings and files

Out-File

Sends output to a file

Out-Null

Deletes output instead of sending it down the pipeline

Out-Grid

Sends output to an interactive table in a separate window

New-Object

Creates an instance of a Microsoft .NET framework or COM object

Write-Host

Writes customized output to a host

Write-Output

Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console

grep is an alias for Select-String

Get-ChildItem -recurse

Goes through an gets all items and their child items (recursively)

Get-ChildItem –recurse | foreach-object{write-host $_.name}

Take the output from the first command as input and iterate through it and write only the objects' name output to the console

Take the output from the first command as input and iterate through it and write only the objects' name output to the console

You can store command output to variables

e.g.

$dir = get-childitem

$dir

  • Assign and store variables with the sigil, $

Typecasting:

e.g.

[xml]$xml

Assigning command output to a variable:

  • Unlike linux, in powershell you don't need to surround a command in `` or $() in order to store command output to a variable

e.g.

$xml = get-content ".\backspace.xml"

Run a script directly from memory:

Last updated