PowerShell Cheat Sheet - Simple Cheat Sheet - Windows PowerShell
PowerShell Cheat Sheet - Simple Cheat Sheet - Windows PowerShell
This cheat sheet includes basic syntax and methods to help you using PowerShell.
PowerShell is a cross-platform task automation and configuration management
framework, consisting of a command-line shell and scripting language.
# Do something if 1 is equal to 1
if (1 -eq 1) { }
# Do something indefinitely
while ($true) { }
Up ↑
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 1/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
'e' { $PSItem }
'r' { $PSItem }
If If($x
eq 5){} Elseif$x
gt 5 Else
While $x = 1; while($x
lt 10){$x;$x++}
For For($i=0; $i
lt 10; $i++){ $i }
Switch
options (){
Up ↑
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 2/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
$a = 0 Initialize a variable
Get-Variable | ? {
$PSItem.Options -contains Get variables with the “Constant” option set
'constant' }
Get-Variable | ? {
$PSItem.Options -contains Get variables with the “ReadOnly” option set
'readonly' }
New-Variable -Name
FirstName -Value Trevor
New-Variable FirstName -
Create a constant variable, that can only be
Value Trevor -Option
removed by restarting PowerShell
Constant
Remove-Variable -Name
Remove a variable, with the specified name
firstname
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 3/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
function Do-Something {
[CmdletBinding]()]
param ()
begin { }
process { }
end { }
$a=1
Do {$a; $a++}
# Do Until Loop
$a=1
Do {$a; $a++}
# For Loop
{$a}
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 4/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
{$i.name; $i.creationtime}
Get-Command
# (native binaries in $env:PATH + cmdlets /
functions from PowerShell modules)
Get-Help -Name Get help for a specific about_* topic (aka. man
about_Variables page)
Up ↑
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 5/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
$FirstName =
'Trevor'
Perform string comparison using the -like operator,
which supports the wildcard (*) character. Returns
$FirstName -like $true
'T*'
Up ↑
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 6/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
\ escape character
\d Digit [0-9]
\D non-digit [^0-9]
\n new line
\r carriage return
\t tabulation
\s white space
\S non-white space
^ beginning of a line
$ end of a line
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 7/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
? matches 1 or 0 times
?? matches 0 or 1 time
#cowColour
#Brown
#Not so wild
#<cow>MyHeading</cow>
#negative lookbehind
Up ↑
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 8/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
cat, gc, type Gets the content of the item at the specified location.
curl, iwr, wget Gets content from a web page on the Internet.
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 9/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
Table of contents
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 10/11
8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell
©
2022 Simple Cheat Sheet < https://simplecheatsheet.com/>
Privacy Policy < https://simplecheatsheet.com/privacy-policy/>
Up ↑
https://simplecheatsheet.com/tag/powershell-cheat-sheet/#powershell-flow-control 11/11