Useful PowerShell Cmdlets

Visit https://adamtheautomator.com/powershell-environment-variables/

Help

Get-Help [cmdlet][-Examples]

Get-help - return information on how to use the help system.

Get-help sort - will return information on the sort cmdlet

Get-help sort -Examples - will return examples on how to use the sort cmdlet

Filtering Results

Sort

Sort the outputs of another command.

Measure-Object

Return really useful information about an object of any kind.

Find what commands are available

get-command

Returns all the commands available on your system

Piping one cmdlet into another

get-command -module packagemanagement | sort noun, verb

This will take the output of get-command which is currently only going to list commands that are related to the package management and pipe it into sort which then sorts it by noun and verb.

The above command could be partially done with - get-command | findstr packagemanagement | sort noun, verb

get-command > allcommands.txt

Get all available cmdlets and store them in a file called allcommands.txt

more .\allcommands.txt

Display all of the contents of the file allcommands.txt to the screen

Get-Content .\allcommands.txt | Measure-Object -Line

Read the contents of the file allcommands.txt, redirect the output to Measure-Object which then counts the number of lines in it

find-package -Source chocolatey

Find all packages provided by chocolatey

find-package | findstr chocolatey | sort > chocolatey-packages.txt

Find all packages provided by chocolatey and save the results in a file called chocolatey-packages.txt

Find a file

Search the file system for a particular file with a particular file name pattern

Get-ChildItem -Path .\ -file -filter *.pem -Recurse

From the current location recursively search for any files ending with .pem

List contents of an environment variable and split them out based on a delimiter

$env:path -split ';'

Run an Application in the background

Start-Process -NoNewWindow ping google.com

Or you can create a reusable shortcut function, and store it in a script file that is then on the PATH

function bg() {Start-Process -NoNewWindow @args}

Find a process listening on a port

netstat -aon | findstr ":80" | findstr "LISTENING"

OR

Get-Process -Id (Get-NetTCPConnection -LocalPort Your_Port_Number).OwningProcess

 

Kill a process by its PID

tasklist /fi "pid eq <PID>"

Run process in the background

First, create a function that can be used to execute processes in the background

Now it can be used as follows

ipconfig will now run in the background