Get-host | select-object Version # to display PowerShell version
$PSVersionTable.PSVersion # display PowerShell version
Get-Command -Verb Get # to list all cmdlets that uses the verb Get
Get-Command -Noun service # to list all commands available to manage services.
Get-Command *-Service # find a list of cmdlets with service in their name
Get-Command -Name Clear-Host
Get-Help or help or man # to get help
Get-Help Get-Service -example # Get help for cmdlet Get-Service
Get-Verb # show all the verbs
Clear; dir *.txt # multiple commands separated by semicolon
write-host "Hello World" # Famous hello world example
write-host -foregroundcolor red "Hello World"
Linux commands that can be used as such in PowerShell "cat, cd, chdir, clear, cls, copy, del, diff, dir, echo, erase, h, history, kill, lp, ls, mount, move, popd, ps, pushd, r, ren, rm rmdir, sleep, sort, tee, type, write,
Get-Item env: # show the environment variables
Get-Location # Equivalent to command 'pwd' in Unix
Get-ChildItem # Equivalent to "ls" in Unix
Get-Alias ls # Find the alias for ls command
Get-Alias | out-host -paging # Equivalent to more
Get-Alias | more # more
Get-ChildItem $env:USERPROFILE\Desktop | Sort-Object -Property LastWriteTime (Equivalent to ls -ltr in Unix)
Get-ChildItem -Filter "commands" -Recurse -File (Equivalent to find command in Unix)
cd $env:USERPROFILE # Equivalent to command "cd $HOME" in Unix
Remove-Item -Recurse -Force # Eqivalent to "rm -rf" in Unix.
New-Item -ItemType Directory -Name 'TestFolder' # Equivalent to "mkdir" in Unix
New-Item -ItemType File -Name 'TestFile' # Equivalent to "touch" in Unix
Get-Content <filename> # Equivalent to "cat" in Unix
Get-Content -Tail 2 <filename> # Display last two lines
Get-Content .\Alias.txt | select -first 10 # Equivalent to head -10 ; <Alias.txt> is a filename, replace that with your file name
Get-Content .\Alias.txt | select -last 10 # Equivalent to last -10
Get-Content -totalcount 10 .\Alias.txt # Equivalent to head -10
Get-Content .\Alias.txt | %{ $_ -replace '\d+', '($0)' } # Equivalent to sed; "%" is an alias for ForEach object "Get-Alias -Definition ForEach-Object"; It will also be a modulus operator like in 10 % 3.
Get-Content .\Alias.txt | %{$n=0;}{$n++;"$n $_"} # Equivalent to cat -n
Get-Content .\Alias.txt | ForEach{ "{0,5} {1}" -f $_.ReadCount, $_ } # print with line numbers
Get-Content .\Alias.txt | %{$n=0;}{$n++; if($n -gt 2 -and $n -lt 7){"$n $_"}} # Equivalent to sed to print certain lines
Measure-Command { Get-EventLog "windows powershell" } # Equivalent to time command in Unix
Get-ChildItem | Measure-Object # count the number of files
Get-Process | where-object cpu -gt 100 # Display process using more than 100% CPU. Instead of get-process you can type "ps"
(Get-Command python).Path # Equivalent to "which python" in Unix
Get-Ciminstance win32_operatingsystem # information about your operating system
Get-CimInstance -Class Win32_Process | more # process information
Test-Connection <ipaddress> # equivalent to ping
Select-String -Path .\commands "process" # Equivalent to "grep" in Unix; commands is a filename, replace it with your file name and string.
Get-Help Get-Service -ShowWindow # Pop up the help in a separate window
Get-Process | Where-Object {$_.WorkingSet -gt 20000000} # Display process greater than certain size.
Get-Process powershell # Display details of the process powershell
Get-Process | Where {$_.Handles -gt 750} | Sort PM -Descending # sort the output
Get-Process -IncludeUserName # Run it as Administrator to get the process username information
$process = (Get-Process)[0] # subExpressions
"Net*" | Get-Service # Show all services with name Net
Get-Service -Displayname "sql*" # Show all the services with displayname sql
Get-Service | Where-Object {$_.Status –eq “Running”} # Show all running jobs.
Get-ComputerInfo | more # info about your computer.
Get-Service | out-file -FilePath C:\Users\<username>\service.txt # Store the output to a file
Get-Service | export-csv -Path C:\Users\<username>\service.txt # output to CSV format.
Get-EventLog -LogName System -Newest 10 -EntryType error # show event logs
Get-Service | Select-Object -Property name, status | Sort-Object -property status -Descending # Show running and stopped services sorted.
Get-ChildItem -path C:\Users\<username>\ | Sort-Object -Property length -Descending # Show the files sorted according to size
Get-ExecutionPolicy # Show execution policy (Restricted, RemoteSigned etc.)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # Set execution policy
Get-CimInstance -ClassName Win32_LogicalDisk -filter "DeviceID='c:'" | select @{n="FreeGB"; e={$_.FreeSpace / 1gb -as [int]}} # Show the free space in GB
Get-CimInstance -ClassName Win32_LogicalDisk -filter "DeviceID='c:'" | Select-Object -Property @{n="FreeGB"; e={$_.FreeSpace / 1gb -as [int]}} # show the free space in GB and change the column heading as well.
ise # start powershell ise (Other commands are notepad, calc etc )
Get-History # Equivalent to history command in Unix
Get-History | Foreach-Object { $_.CommandLine } > .\history_script.ps1 # save the history to a file.
New-Alias -Name grep -Description grep Select-String # Alias for grep in Unix
Get-ChildItem env: | out-host -paging # Equivalent to less command in Unix
dir –r | Select-String "searchforthis" # Recursively search for a string.
ps | sort –p ws | select –last 5 # Find the five process using most memory
ps | sort –p cpu | select –last 5 # Find the five process using most CPU
Get-WmiObject -Class Win32_LogicalDisk # show information about the hard drive free space and total space.
Get-WmiObject -Class Win32_ComputerSystem # Get information about the model of this computer
Get-WmiObject -Class Win32_BIOS -ComputerName . # Get BIOS information
Get-WmiObject -Class Win32_ComputerSystem -Property UserName -ComputerName . # Get the information about the user logged into the machine.
Get-WmiObject -Class Win32_Product -ComputerName . | Format-Wide -Column 1 # List all the applications that are installed on this computer
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Format-Table -Property IPAddress # List all the IP address assigned to this computer.
Get-WmiObject -class Win32_OperatingSystem # operating system information
Get-Netadapter # Get detailed information about the network adapters, the mac address, link speed, interface description etc.
Get-Netadapter | format-table -Autosize
Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4'} | ForEach-Object IPAddress # Get all the IPV4 addresses.
Get-Eventlog application -newest 5 | format-table -Wrap # to Wrap the output
New-Alias grep findstr # use grep (like in unix) for findstr
ls | grep txt # display all files with "txt" string in it.
'Hello', 'HELLO' | Select-String -Pattern 'HELLO' -CaseSensitive -SimpleMatch # select-string can also provide grep like functionality
Set-Location c:\ # Equivalent to "cd" in Unix
Start-Process ((Resolve-Path "C:\Users\<username>\file.pdf").Path) # open a pdf file
Find-Package <pkgname> # find packages from a repository Eg: Find-Package ssh | Install-Package as administrator
Get-PSDrive # equivalent to df command
Measure-Command { Get-EventLog "windows powershell" } # Equivalent to time command