PowerShell Cheatsheet

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
dir
get-childitem
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
help
get-command
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:\
d:
cd \ 
set-location d:\
alias: cd d:\ 
Clear the screen
cls
clear-host
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
cd
get-location
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
set
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 
get-service
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
tasklist
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:
dir
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
(dir).count
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).

13 Responses to “PowerShell Cheatsheet”

  1. james stokes Says:

    What i would like to do is:
    1- set a variable with string of the directory we want to use
    2- set a filename of outfile.sql
    2- gather the content of all files with the extension of “*.sql” merge into outfile.txt

  2. Steinam Says:

    $directory = “C:\sql”
    $output = “C:\outfile.sql”

    #ErrorAction enum if File does not exist
    Clear-Content $output -ErrorAction “SilentlyContinue”

    $list = Get-ChildItem -Path $directory -Filter *.sql
    foreach ($i in $list)
    {
    $a = get-content $i.FullName
    add-Content $a -Path $output
    Add-Content “– ========== next sql –” -Path $output
    }

  3. Mike Ormond's Blog : Powershell Batch Rename Says:

    [...] found plenty of help on the web – or at least plenty of suggestions for renaming and batch renaming. But everything I tried gave me [...]

  4. mycall Says:

    One important thing left off that I can’t found:

    cd \windows
    dir cmd.exe /s

    What is PS’s equivalent?

  5. Jon Saxton Says:

    I note that PS honours my UI setting for date format (YYYY-MM-DD) in directory listings but it insists on using am/pm for file modification times even though I have told Windows to use a 24-hr clock. So far I have not figured out a workaround for that. Given that the date works, could this be a bug or an oversight?

  6. PowerShell Cheat Sheet - InterVirt Says:

    [...] http://blog.stevex.net/index.php/powershell-cheatsheet/ [...]

  7. Jayson Gabler Says:

    Does anyone know to:

    dir .. (go up one directory)
    output the name of a directory to a variable that can be used for comparison
    Detect when you’ve reached the end of a directory tree, i.e. C:\Dir1\Dir2\Dir3 (stop at Dir3)
    Detect when you’ve reached the start of a directory tree, i.e. C:\Dir1\Dir2\Dir3 (stop at C:\)

  8. Jayson Gabler Says:

    Actually, I figure out how to go up a directory, dir .. worked funnily enough!!!

  9. Adrian Rodriguez Says:

    Hi Steve,

    What if I want to delete directories older than 1 week old?

    -Adrian

  10. Niklas E Says:

    Anyone with a command that can give me a list of all files that do NOT contain SUCCESS inside the text file for all result.txt files under C:\batches\* (recursive subfolders) . Since all files have same name, but are in separate folder, I guess I need full path name as well. I have been able to do the reverse to find all that contain SUCCESS, but not the ones that do not contain it.

    get-childitem C:\batches\* -include result.txt -recurse | select-string -pattern “SUCCESS” -list | Select-Object Path

    I also tried NotMatch but it didn’t work as it finds all files as there are rows within the files that don’t have that line.

    Any ideas?

  11. Jickson sebastian Says:

    Hi,

    Is there a command to search for a particular file with todays date in a folder?

  12. Mischa Says:

    Get-ChildItem \\omega\test2$\*.txt | % { If (( Get-Content $_) -Match “Test”) { $_ } }

  13. Justin Says:

    Update on Mischa’s script:

    Get-ChildItem C:\batches -include result.txt -recurse |? { -not $($(Get-Content $_) -match “SUCCESS”) }

    “|?” is a short-cut for “| Where-Item”, which is basically what the for-each command Mischa wrote is doing.

Leave a Reply

Powered by WP Hashcash