I’ve gathered a very basic list of some common and entry level powershell commands listed below..

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}

Advertisements