PowerShell Essential Cheat Sheet

Youssef Idlahsen

Youssef IdlahsenFeb 5, 2023

5 min read844 words

What is PowerShell?

PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS.

What are the PowerShell Commands?

In PowerShell, commands are referred to as "cmdlets" and are executed by typing them into the console and pressing Enter. Commands are written in verb-noun form, and named parameters start with a dash.

Basic knowledge

Cmdlet Commands built into shell written in .NET
Functions Commands written in PowerShell language
Parameter Argument to a Cmdlet/Function/Script
Alias Shortcut for a Cmdlet or Function
Scripts Text files with .ps1 extension
Applications Existing windows programs
Pipelines | Pass objects Get-process word | Stop-Process
Ctrl+c Interrupt current command
Left/right Navigate editing cursor
Ctrl+left/right Navigate a word at a time
Home / End Move to start / end of line
Up/down Move up and down through history
Insert Toggles between insert/overwrite mode
F7 Command history in a window
Tab / Shift-Tab Command line completion

PowerShell Cmdlets List

Help

Commands Functions
Get-Command Get all commands
Get-Command -Module RGHS Get all commands in RGHS module
Get-Command Get-p* Get all commands starting with get-p
Get-help get-process Get help for command
Get-Process | Get-Member Get members of the object
Get-Process| format-list -properties * Get-Process as list with all properties

Cmdlets

Get-EventLog Get-WinEvent Get-Date
Start-Sleep Compare-Object Start-Job
Get-Credential Test-Connection New-PSSession
Test-Path Split-Path Get-ADUser
Get-EventLog Get-WinEvent Get-Date
Get-ADComputer Get-History New-ISESnippet
Get-WMIObject Where-Object Get-CimInstance
Set-Content Get-Content Add-Content
Set-Location Where-Object Get-CimInstance
Out-File Out-String Copy-Item
Remove-Item Move-Item New-Item

Assignment, Logical, Comparison Operators

Commands Functions
=,+=,-=,++,-- Assign values to variable
-and,-or,-not,! Connect expressions / statements
-eq, -ne Equal, not equal
-gt, -ge Greater than, greater than or equal
-lt, -le Less than, less than or equal
-replace “Hi” -replace “H”, “P”
-match,-notmatch Regular expression match
-like,-notlike Wildcard matching
-contains,-notcontains Check if value in array
-in, -notin Reverse of contains, notcontains

Parameters

Commands Functions
-Confirm Prompt whether to take action
-WhatIf Displays what command would do

Variables

Commands Functions
$var = "string" Assign variable
$a,$b = 0 or $a,$b = 'a','b' Assign multiple variables
$a,$b = $b,$a Flip variables
$var=[int]5 Strongly typed variable

Arrays, Objects

Commands Functions
$arr = "a", "b" Array of strings
$arr = @() Empty array
$arr[5] Returns the sixth array element, where the index of the first item is 0
$arr[-3..-1] Last three array elements
$arr[1,4+6..9] Elements at index 1,4, 6-9
$arr[1] += 200 Add to array item value
$z = $arA + $arB Two arrays into single array
(Get-Date).Date Date property of object

Importing, Exporting, Converting

Export-CliXML Import-CliXML ConvertTo-XML
Export-CSV Import-CSV ConvertTo-CSV
ConvertFrom-CSV ConvertTo-HTML

Comments, Escape Characters

Commands Functions
#Comment Comment
<#comment#> Multiline Comment
`t Tab
`n New line
` Line continue

Aliases for common commands

Aliases Commands
Gcm Get-Command
Foreach,% Foreach-Object
Sort Sort-Object
Where,? Where-Object
Diff,compare Compare-Object
Dir, ls, gci Get-ChildItem
Gi Get-Item
Copy,cp,cpi Copy-Item
Move,mv,mi Move-Item
Del,rm Remove-Item
Rni,ren Rename-Item
Ft Format-Table
Fl Format-List
Gcim Get-CimInstance
Cat,gc,type Get-Content
Sc Set-Content
h,history,ghy Get-History
Ihy,r Invoke-History
Gp Get-ItemProperty
Sp Set-ItemProperty
Pwd,gl Get-Location
Gm Get-Member
Sls Select-String
Cd,chdir,sl Set-Location
Cls,clear Clear-Host

Conclusion

PowerShell provides a rich set of built-in tools for scripting, including a scripting language and integrated debugger, making it easy for users to write and test scripts. PowerShell also has a robust community and extensive resources, including online documentation, forums, and blogs, which provide support and information for users of all skill levels.

Overall, PowerShell is a powerful tool that can significantly increase productivity and simplify complex IT tasks, making it an essential tool for system administrators and power users.

Youssef's Newsletter

Trust me, I won't spam you.

Share on Social Media: