PowerShell Commands Cheat Sheet

Posted by SysAdmin Tools on

 

Finding other commands

Get-Command *topic*
For Example : Get-Command *rename*
This will give you all the commands related to renaming items. 

Search for files and folders

Get-ChidItem C:\ -Force - Recurse
This will search for all files and folders including subfolders (recurse) and hidden files (force) in C:\Temp

Show more properties for a result

Get-Service | fl*

Filtering on results using Where

Get-Service | where {$_.StartType -eq "Automatic" -and $_.Status -ne "Running" }
This will show all the services that are set to Automatic, but are not Running

Select only the data you need

Get-Service | Select Name, Status, StartType
This will select only the Name, Status and the service StartType

Export the data to a file

ping 8.8.8.8 -t | Out-File C:\Temp\Ping8.8.8.8.txt
This is exactly the same as the ">" character in command prompt.
You can add the -Append switch if you don't want to override the current contents of the file. 

Export results to a CSV

Get-Service | Select Name, Status, StartType | Export-Csv C:\Temp\ServiceFile.csv -Delimiter ";"
This will export the services to CSV using a semicolon delimiter 
Try not to select too many items. 

Sorting

Get-Process | Sort-Object Handles -Descending
This will show you the processes with the most Handles first

Select only the first or last X results

Get-Process | Sort-Object Handles -Descending | Select -First 5
This will only show you the first 5 results. 

Get-Process | Sort-Object Handles -Descending | Select -Last 5
This will only show you the last 5 results. 

select a specific row

(Get-Process | Sort-Object Handles -Descending)[0]
Selects the first row.
This is useful when testing foreach statements. 

get a property from a query into a string without heading

$Value = (Get-Process)[0].Handle
This will add only the value of the handle into the Variable. 

Get-WMI Values

This command is quite valuable.
Almost any value or property you can think of for monitoring should be available via WMI 

For example. 
The same query to check for Auto services that are not Running. 

Get-WmiObject -Query "SELECT * From Win32_Service where StartMode='Auto' and State<>'Running'"

You can also do the filtering with a Where clause as seen previously. 

To get a list of useful classes, either Google or use this command 
Get-WmiObject -List -Class *Win32_Ser*
This command will show you the service Class

Viewing in a nice table

Get-WmiObject -Query "SELECT * From Win32_Service where StartMode='Auto' and State<>'Running'"  | ft -AutoSize

Random Values

This uses a DotNet Class, but still works 100%

[System.Random]::new().Next(1,1000)
This gives you a random number between 1 and 1000

Script Delay 

sleep 5
This will add a 5 second delay in your script.

See all the properties of an object 

Get-ChildItem C:\Temp\ServiceFile.csv | gm
This will show all the properties and actions you can take on the Get-ChildItem command. 

WhatIf

If you are not sure what action a PowerShell command will take and you dont want to break anything, you can add the command -WhatIf to the end of your action. 

Get-Service | where {$_.StartType -eq "Automatic" -and $_.Status -ne "Running" } | Start-Service -WhatIf

This will list all the actions that will be taken when that command is run, but it won't actually do them.

Get a registry value

Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
That will list all the registry entries that contain the installed program values. 

Read a file

Get-Content C:\Temp\ServiceFile.csv

Search a file

Get-Content C:\Temp\ServiceFile.csv | Select-String "Auto" | Select-String "Stopped"
That will show you all the lines in the file we exported earlier that contain the text "Auto" and "Stopped"

ForEach

Get-Service | foreach {$_.Name}

Check if file exists

Test-Path C:\Temp\ServiceFile.csv

if

If True
if (Test-Path C:\Temp\ServiceFile.csv -eq $true) {"File Exists" }
Default is $true, so "-eq $true" not required

If False
if (Test-Path C:\Temp\ServiceFile.csv -eq $false) {"File does not Exists" }

Take Action
if (Test-Path C:\Temp\ServiceFile.csv) {"Create File" | Out-File C:\Temp\ServiceFile.csv}

foreach with range 

1..254 | foreach {ping 192.168.0.$_ -n 1}
Ping 192.168.0.1 - 192.168.0.254

Make a beep 

[System.Console]::Beep(1000,1000)
Beep(Frequency,Duration)

WHILE

Wait for a DNS record to update and beep when it reflects. 
While name.domain.com does not resolve on DNS Server 1.1.1.1 to IP Address 1.2.3.4 , Beep

name.domain.com  = the domain you are checking
1.1.1.1 = The google server you are checking against. 
-ne = Not Equal
1.2.3.4 = the IP you want it to be

while ((Resolve-DnsName name.domain.com -Server 1.1.1.1).IPAddress -ne "1.2.3.4") { } else {[System.Console]::Beep(2000,1000) }

BONUS TIP

To find more commands and functions, simply run this command in PowerShell and it will show all the above commands and more!

Get-Command | where {$_.Source -like "Microsoft.PowerShell.*" } | sort Source | sort Version

 

if you found this useful, please look at our free and premium tools and consider becoming a member for as little at $2pm or $19 once off. 

Members get access to

  • All the tools

  • Feature requests

  • Support.


Share this post



← Older Post Newer Post →


2 comments

  • Very good sheet, keep on coming

    Angel Cruz on
  • I like the idea, but this is kind hard to look at. If you turn it into a 1-2 page info-graphic printable sheet, like docker and so many other companies do, I think people would find really useful.

    Dave on

Leave a comment

Please note, comments must be approved before they are published.