0% found this document useful (0 votes)
53 views

PowerShell Cheat Sheet - Simple Cheat Sheet - Windows PowerShell

This document contains summaries of various PowerShell cheat sheets covering topics like flow control, variables, functions, loops, essential commands, and operators. It provides code examples and brief explanations for common PowerShell syntax and commands to help users automate tasks and configure their environment using PowerShell.

Uploaded by

Pratik Raman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

PowerShell Cheat Sheet - Simple Cheat Sheet - Windows PowerShell

This document contains summaries of various PowerShell cheat sheets covering topics like flow control, variables, functions, loops, essential commands, and operators. It provides code examples and brief explanations for common PowerShell syntax and commands to help users automate tasks and configure their environment using PowerShell.

Uploaded by

Pratik Raman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

8/3/22, 3:08 AM PowerShell Cheat Sheet - Simple Cheat Sheet | Windows PowerShell

PowerShell Cheat Sheet

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.

PowerShell: Flow Control Cheat Sheet <


https://simplecheatsheet.com/powershell-flow-control/>

# Do something if 1 is equal to 1

if (1 -eq 1) { }

# Loop while a condition is true (always executes at least once)

do { 'hi' } while ($false)

# While loops are not guaranteed to run at least once

while ($false) { 'hi' }

# Do something indefinitely

while ($true) { }

# Break out of an infinite while loop conditionally

while ($true) { if (1 -eq 1) { break } }

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

# Iterate using a for..loop

for ($i = 0; $i -le 10; $i++) { Write-Host $i }

# Iterate over items in an array

foreach ($item in (Get-Process)) { }

# Use the switch statement to perform actions based on conditions.


Returns string 'matched'

switch ('test') { 'test' { 'matched'; break } }

# Use the switch statement with regular expressions to match inputs

# NOTE: $PSItem or $_ refers to the "current" item being matched in


the array

switch -regex (@('Trevor', 'Daniel', 'Bobby')) {

'o' { $PSItem; break }

# Switch statement omitting the break statement. Inputs can be matched


multiple times, in this scenario.

switch -regex (@('Trevor', 'Daniel', 'Bobby')) {

'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 }

Foreach Foreach($file in dir C:){$file.Nam

Foreach 1..10 | foreach{$_}

Switch ­
options (){

Switch PatternX {statement}

Default {Default Statement} 

PowerShell: Variables Cheat Sheet <


https://simplecheatsheet.com/powershell-variables/>

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

Initialize a variable, with the specified type


[int] $a = 'Trevor'
(throws an exception)

Initialize a variable, with the specified type


[string] $a = 'Trevor'
(doesn’t throw an exception)

Get a list of commands related to variable


Get-Command -Name *varia*
management

Get an array of objects, representing the


Get-Variable
variables in the current and parent scopes

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

New-Variable FirstName - Create a variable that can only be removed by


Value Trevor -Option specifying the -Force parameter on Remove-
ReadOnly Variable

Remove-Variable -Name
Remove a variable, with the specified name
firstname

Remove-Variable -Name Remove a variable, with the specified name,


firstname -Force that has the “ReadOnly” option set Up ↑

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

PowerShell: Functions Cheat Sheet <


https://simplecheatsheet.com/powershell-functions/>

# A basic PowerShell function

function add ($a, $b) { $a + $b }

# A PowerShell Advanced Function, with all three blocks declared:


BEGIN, PROCESS, END

function Do-Something {

[CmdletBinding]()]

param ()

begin { }

process { }

end { }

PowerShell: Loop Cheat Sheet <


https://simplecheatsheet.com/powershell-loop/>
# Do While Loop

$a=1

Do {$a; $a++}

While ($a –lt 10)

# Do Until Loop

$a=1

Do {$a; $a++}

Until ($a –gt 10)

# For Loop

For ($a=1; $a –le 10; $a++)

{$a}

# ForEach – Loop Through Collection of Objects


Up ↑

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

Foreach ($i in Get-Childitem c:\windows)

{$i.name; $i.creationtime}

PowerShell: Essential Cheat Sheet <


https://simplecheatsheet.com/powershell-essential-commands/>

# Retrieves a list of all the commands available to


PowerShell

Get-Command
# (native binaries in $env:PATH + cmdlets /
functions from PowerShell modules)

Get-Command -Module Retrieves a list of all the PowerShell commands


Microsoft* exported from modules named Microsoft*

Retrieves a list of all commands (native binaries +


Get-Command -Name *item
PowerShell commands) ending in “item”

Get-Help Get all help topics

Get-Help -Name Get help for a specific about_* topic (aka. man
about_Variables page)

Get-Help -Name Get-


Get help for a specific PowerShell function
Command

Get-Help -Name Get-


Get help for a specific parameter on a specific
Command -Parameter
command
Module

PowerShell: Operators Cheat Sheet <


https://simplecheatsheet.com/powershell-operators/>

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

$a = 2 The basic variable assignment operator

$a += 1 Incremental assignment operator

$a -= 1 Decrement assignment operator

$a -eq 0 Equality comparison operator

$a -ne 5 Not-equal comparison operator

$a -gt 2 Greater than comparison operator

$a -lt 3 Less than comparison operator

$FirstName =
'Trevor'
Perform string comparison using the -like operator,

which supports the wildcard (*) character. Returns
$FirstName -like $true
'T*'

PowerShell: Regular Expressions Cheat Sheet <


https://simplecheatsheet.com/powershell-regular-expressions/>

Below is a regular expression list

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

. matches any character except newline

\ escape character

\w word character [a-zA-Z_0-9]

\W non-word character [^a-zA-Z_0-9]

\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

\A beginning of the string (multi-line match)

\Z end of the string (multi-line match)

\b word boundary, boundary between \w and \W

\B not a word boundary

\< beginning of a word

\> end of a word

{n} matches exaclty n times

{n,} matches a minimum of n times

{x,y} matches a min of x and max of y


Up ↑
(a|b) ‘a’ or ‘b’

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 0 or more times

+ matches 1 or more times

? matches 1 or 0 times

*? matches 0 or more times, but as few as possible

+? matches 1 or more times, but as few as possible

?? matches 0 or 1 time

#Match while tagging match groups

'CowColour Brown' -match '(?<Attribute>\w+) (?<Value>\w+)' | out-null


$matches.Attribute $matches.Value

#cowColour

#Brown

#Matching groups - your $matches object will have properties


containing the valid matches

"Subnet:10.1.1.0/24" -match 'Subnet:(?<SiteSubnet>(?:\d{1,3}\.)


{3}\d{1,3}/\d+)'

#Replace to reformat a string

'This is a wild test' -replace '.*(w[^ ]+).*','Not so $1'

#Not so wild

#Lazy matching (to prevent over-matching) use a ? after the + or *

"<h1>MyHeading</h1>" -replace '<([^/]+?)>','<cow>' -replace


'</([^/]+?)>','</cow>'

#<cow>MyHeading</cow>

#negative lookbehind

($DistinguishedName -split '(?<!\\),')[1..100] -join ','

PowerShell: Commands List Cheat Sheet <


https://simplecheatsheet.com/powershell-commands-list/>

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

Sets the current working location to a specified


cd, chdir, sl
location.

cat, gc, type Gets the content of the item at the specified location.

Adds content to the specified items, such as adding


ac
words to a file.

Writes or replaces the content in an item with new


sc
content.

copy, cp, cpi Copies an item from one location to another.

del, erase, rd, ri,


Deletes the specified items.
rm, rmdir

mi, move, mv Moves an item from one location to another.

Changes the value of an item to the value specified in


si
the command.

ni Creates a new item.

sajb Starts a Windows PowerShell background job.

compare, dif Compares two sets of objects.

Groups objects that contain the same value for


group
specified properties.

curl, iwr, wget Gets content from a web page on the Internet.

Calculates the numeric properties of objects, and the


measure characters, words, and lines in string objects, such as
files …

Resolves the wildcard characters in a path, and


rvpa
displays the path contents.

rujb Restarts a suspended job

Sets the value of a variable. Creates the variable if


set, sv Up ↑
one with the requested name does not exist.

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

shcm Creates Windows PowerShell commands in a


graphical command window.

sort Sorts objects by property values.

sasv Starts one or more stopped services.

saps, start Starts one or more processes on the local computer.

sujb Temporarily stops workflow jobs.

Suppresses the command prompt until one or all of


wjb the Windows PowerShell background jobs running in
the session are …

Selects objects from a collection based on their


?, where
property values.

Sends the specified objects to the next command in


echo, write the pipeline. If the command is the last command in
the pipeline,…

Table of contents

PowerShell: Flow Control Cheat Sheet

PowerShell: Variables Cheat Sheet

PowerShell: Functions Cheat Sheet

PowerShell: Loop Cheat Sheet

PowerShell: Essential Cheat Sheet

PowerShell: Operators Cheat Sheet

PowerShell: Regular Expressions Cheat Sheet

PowerShell: Commands List Cheat Sheet

Nginx Cheat Sheet < https://simplecheatsheet.com/tag/nginx-cheat-sheet/> Up ↑

Java Swing Cheat Sheet < https://simplecheatsheet com/tag/java-swing-cheat-

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

Java Swing Cheat Sheet < https://simplecheatsheet.com/tag/java-swing-cheat-


sheet/>

Heroku Cheat Sheet < https://simplecheatsheet.com/tag/heroku-cheat-sheet/>

Database Cheat Sheet < https://simplecheatsheet.com/tag/database-cheat-sheet/>

MariaDB Cheat Sheet < https://simplecheatsheet.com/tag/mariadb-cheat-sheet/>

Svelte Cheat Sheet < https://simplecheatsheet.com/tag/svelte-cheat-sheet/>

Scala Cheat Sheet < https://simplecheatsheet.com/tag/scala-cheat-sheet/>

DRF Cheat Sheet < https://simplecheatsheet.com/tag/drf-cheat-sheet/>

Jquery UI Cheat Sheet < https://simplecheatsheet.com/tag/jquery-ui-cheat-sheet/>

Golang Cheat Sheet < https://simplecheatsheet.com/tag/golang-cheat-sheet/>

Sql Server Cheat Sheet < https://simplecheatsheet.com/tag/sql-server-cheat-


sheet/>

Nano Cheat Sheet - Window < https://simplecheatsheet.com/tag/nano-cheat-sheet-


window/>

Sql Cheat Sheet < https://simplecheatsheet.com/tag/sql-cheat-sheet/>

Nano Cheat Sheet < https://simplecheatsheet.com/tag/nano-cheat-sheet/>

TensorFlow Cheat Sheet < https://simplecheatsheet.com/tag/tensorflow-cheat-


sheet/>

Linux Command Line Cheat Sheet < https://simplecheatsheet.com/tag/linux-


command-line-cheat-sheet/>

Apache Cheat Sheet < https://simplecheatsheet.com/tag/apache-cheat-sheet/>

Python Cheat Sheet < https://simplecheatsheet.com/tag/python-cheat-sheet/>

Travis CI Cheat Sheet < https://simplecheatsheet.com/tag/travis-ci-cheat-sheet/>

BabylonJS Cheat Sheet < https://simplecheatsheet.com/tag/babylonjs-cheat-


sheet/>

©
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

You might also like