PowerShell Cheat Sheet

Common PowerShell commands and basic constructs.

Files and Folders

Set-Location path - Move to directory [Alias: cd]
Get-ChildItem path - List contents of a folder [Alias: gci, dir, ls]
New-Item name - Create file [Alias: ni]
New-Item name -ItemType Directory - Create directory [Alias: ni]
Rename-Item name newname - Rename a file or directory [Alias: ren, rni]
Remove-Item name - Delete a file or directory [Alias: del, erase, rm, rmdir, ri, rd]
Copy-Item name newname - Copy a file or folder [Alias: cp, copy, cpi]
Move-Item name newfolder - Move a file or folder [Alias: mi, move, mv]
Get-Content name - Show contents of a file [Alias: gc]

Services and Processes

Get-Service - List all services [Alias: gsv]
Get-Service name - Get details about a specific serviceĀ [Alias: gsv]
Start-Service name - Start a stopped service [Alias: sasv]
Stop-Service nameĀ - Stop a running service [Alias: spsv]
Restart-Service name - Restart a running service
Get-Process - List all processes [Alias: ps]
Get-Process name - Get details about a specific process [Alias: ps]
Start-Process name - Start a new process [Alias: start, saps]
Stop-Process name - Stop a process [Alias: kill, spps]
Wait-Process name - Wait for a process to stop before continuing
Debug-Process name - Attach a debugger to a process

Networking

Get-NetIPConfiguration - Show local interfaces and their assigned IPs
Test-Connection name or ipaddr - Basic ping
Test-NetConnection name or ipaddr - Advanced ping
Test-NetConnection name or ipaddr -TraceRoute - Perform a trace route
Test-NetConnection name or ipaddr -Port port - Perform a port check
Set-DnsClientServerAddress -InterfaceAlias interface -ServerAddresses dns,dns2 - Change DNS servers
Resolve-DnsName name or ipaddr - Basic DNS lookup
Resolve-DnsName name or ipaddr -Type recordtype - DNS lookup for a specific record (ie txt, srv, soa, etc)
Clear-DnsClientCache - Clear the local DNS cache

Remoting

Enable-PSRemoting - Enable PowerShell remoting locally
Enter-PSSession computername -Credential username - Connect to a remote computer
Invoke-Command -ComputerName computername -ScriptBlock {commands to execute} -Credential username - Execute the script block contents on the remote computer
$SessionName = New-PSSession computername -Credential username - Store a persistent session in a variable which can be used with Enter-PSSession or Invoke-Command

System Management

Restart-Computer - Reboot machine
Stop-Computer - Shutdown machine
Checkpoint-Computer - Create a system restore point
Restore-Computer - Restore computer to a checkpoint

System Info

Get-ComputerInfo - Large amount of system info (win ver, edition, bios version, etc)
$PSVersionTable - Show PowerShell version
Get-WMIObject Win32_OperatingSystem - Show Windows version
$env:COMPUTERNAME - Show computer name

Constructs

if / else
To check a condition in a script or function, use an If statement. The If can check many types of conditions, including the value of variables and the properties of objects.
if (<Condition>) {
    # Perform action
}
elseif (<Condition>) {
    # Perform action
}
else {
    # Perform action
}
switch
To check multiple conditions, use a Switch statement. The Switch statement is equivalent to a series of If statements, but it is simpler. The Switch statement lists each condition and an optional action. If a condition obtains, the action is performed.
switch (<Variable>) {
    'value1' { # Actions }
    'value2' { # Actions }
    'value3' { # Actions }
    Default { # Actions }
}
foreach
The Foreach statement (also known as a Foreach loop) is a language construct for stepping through (iterating) a series of values in a collection of items. The simplest and most typical type of collection to traverse is an array. Within a Foreach loop, it is common to run one or more commands against each item in an array.
foreach (<Item> in <Collection>) {
    # Perform Action
}
for
The For statement (also known as a For loop) is a language construct you can use to create a loop that runs commands in a command block while a specified condition evaluates to true.
for (<Int>; <Condition>; <Repeat>) {
    # Perform Action
}
do..while, do..until
Runs a statement list one or more times, subject to a While or Until condition.
do {
    # Perform Action
} until (<Collection>)

do {
    # Perform Action
} while (<Collection>)
try / catch
Use Try, Catch, and Finally blocks to respond to or handle terminating errors in scripts.
try {
    # Action
}
catch {
    # Catch terminating errors
}
finally {
    # Performed whether success or error
}
function
A function is a named block of code that performs an action. When you type the function name, the code in the function runs.
function  (<Parameters>) {
    # Actions
}
filter
A filter is a named block of code that establishes conditions for an action. You can type the name of the filter in place of the condition, such as in a Where-Object command.
filter  (<Parameters>) {
    # Actions
}

Display Options

cmd | Format-List - Display as list [Alias: fl]
cmd | Format-Table - Display as table [Aliaas: ft]

Sort and Filter

cmd | Select properties - Select only the listed properties
cmd | Where filter - Select a subset of objects
cmd | Sort property - Sort by property

Comparison Operators

what -operator what

  • -eq Equal
  • -ne Not equal
  • -lt Less than
  • -le Less than or equal
  • -gt Greater than
  • -ge Greater than or equal
  • -like wildcard comparison
  • -notlike wildcard comparison

Keyboard Shortcuts

TabComplete cmdlet or parameter
  
EnterCopy highlighted text
Ctrl + VPaste
  
HomeCursor to begining of line
EndCursor to end of line
EscClear current line
  
Navigate command history
Navigate command history
Alt+F7Clear command history