PowerShell Cheatsheet

Posted by Drothism on Dec 11th, 2008

PowerShell is Microsoft’s replacement for cmd.exe, the venerable NT command shell. It breaks new ground, introducing a powerful but completely new syntax. (If you don’t have it yet, download it here).

Revolutionary but different, users new to PowerShell will often find themselves lost - not being able to figure out how to do simple things that they’ve been doing in other shells for the last 20 years. This cheat sheet should help.

PowerShell’s native commands are all based on a verb-noun syntax, for example, “get-childitem”. Command names are often quite verbose, so there is an extensive list of default aliases that help with common commands. The table below will give the alias, where available, as well as the native PowerShell command.

Operation cmd PowerShell
Get a simple directory listing
alias: dir
Get a recursive directory listing
dir /s
get-childitem -recurse
alias: dir -r
Get a wide directory list
dir /w
dir | format-wide
alias: dir | fw
List built-in commands
alias: help
Copy a file
copy foo.txt bar.txt
copy-item foo.txt bar.txt
alias: copy foo.txt bar.txt
Move a file
move foo.txt c:\
move-item foo.txt d:\
alias: move foo.txt d:\
Rename a file
ren foo.txt bar.txt
rename-item foo.txt bar.txt
alias: ren foo.txt bar.txt
Batch rename
ren *.one *.two
dir *.pdf | rename
  -newname {$_.name -rep ".one",".two"}
Set the current directory to d:\
cd \
set-location d:\
alias: cd d:\
Clear the screen
alias: cls
List only directories
dir /ad
dir | where { $_.MshIsContainer }
Directory list, sorted by date
dir /od
dir | sort-object LastWriteTime
Directory list, sorted by date, descending order
dir /o-d
dir | sort-object LastWriteTime -desc
Show the current directory
alias: pwd
See a command’s help
dir /?
get-help get-command
or: get-help get-command -detailed
or: get-help get-command -full
or: dir -?
List environment variables
dir env:
Delete a file
del foo.txt
remove-item foo.txt
alias: del foo.txt
Find all *.txt files
dir /s *.txt
get-childitem -recurse -include *.txt
alias: dir -r -i *.txt
Find all *.txt files containing a particular string
findstr "foo" *.txt
dir *.txt | select-string "foo"
Show a list of services
net start
Start a service
net start MyService
start-service MyService
Stop a service
net stop MyService
stop-service MyService
Show network shares
net share
gwmi Win32_Share
Show a list of running processes
get-process alias: ps
Kill all notepad.exe processes
taskkill /im notepad.exe /f
ps notepad | kill

A few PowerShell commands that you can’t easily do with the standard Windows shell:

Operation PowerShell
Set the current directory to a UNC path
cd \\\\myserver\\\myshare
Get a list of event logs
get-eventlog -list
View entries in a particular event log
get-eventlog -newest 20 -logname System
Treat the registry like a filesystem
cd hkcu:
Recursive directory, grouped by extension
dir -r | group extension
Search for a file containing a string, recursive
dir -r | select-string "foo"
List the 10 processes using the most memory
ps | sort -p ws | select -last 10
Count the results of a directory listing
Count the results of a directory listing
$f = Get-Content "myfile.txt"
foreach ($item in $f)
{do stuff with $item}

There’s a lot to PowerShell, and this only scratches the surface. This guide is meant to help you get going with some everyday commands, but be sure to read some of these great PowerShell blogs, and the PowerShell Script Center for more in-depth tips. (And here’s a good reference on PowerShell syntax).

Leave a reply

You must be logged in to post a comment.