PowerShell
Getting Help
Open the online documentation page for the
Get-Helpcommand:Get-Help -Name Get-Help -Online
Functions
Output a list of accepted verbs to use when naming a function:
Get-Verb | Sort-Object -Property Verb
File Management
Create an empty file
file.txt# Alias: "ni" New-Item 'file.txt'Create a copy of file
old.txtlocated atnew.txt:# Alias: "cp" Copy-Item 'old.txt' 'new.txt'Create a symbolic link to a file
target.txtlocated atlink.txt:# Alias: "ni" New-Item ` -ItemType SymbolicLink ` -Value 'target.txt' ` -Path 'link.txt'Remove a file located at
target.txt# Alias: "rm" Remove-Item 'target.txt'Remove a directory located at
dir:# Alias: "rm" Remove-Item -Recurse 'dir'Force remove a directory located at
dir:# Alias: "rm" Remove-Item -Recurse -Force 'dir'
Input/Output
Output the contents of
file.txtto the console:# Alias: "cat" Get-Content 'file.txt'
Clipboard Manipulation
Set the contents of the clipboard to
hello world:Set-Clipboard -Value 'hello world'Output the contents of the clipboard to the console:
Get-Clipboard
Useful Commands
Open the File Explorer in the current directory:
# Alias: "ii" Invoke-Item .Open a URI using the default handler:
Launching the calculator:
Start-Process -FilePath 'calculator://'Launching the Windows Store:
Start-Process -FilePath 'ms-windows-store://home'Viewing a photo in the native Photos application:
Start-Process -FilePath 'ms-photos:photo?file=FILEPATH/photo.jpg'
Join together multiple components of a path:
$(Join-Path "one" "two" "three.txt")one\two\three.txtReplace the component of the current directory corresponding to the home directory with
~:$(Get-Location).ToString().Replace($HOME, "~")Create a new alias
vito invoke the NeoVim editor:New-Alias -Name "vi" -Value "nvim.exe" -Description "NeoVim" -Option ReadOnlyTest if a network connection can be formed with a remote host on a particular port:
Test-NetConnection ` -ComputerName example.com ` -CommonTCPPort SMBOpen a link in the browser:
Start-Process 'http://localhost:80'Identify the process running on port 80:
Get-Process -Id (Get-NetTCPConnection -LocalPort 80).OwningProcess
Formatting Strings
I stumbled upon a useful article: How-to: The -f Format operator
As practice, I wrote the following command, which prints the elapsed when launching a new instance of PowerShell, up to the first 3 decimal places:
"{0:n3}s" -f (Measure-Command { pwsh -NoLogo -NoProfile -Command 1 }).TotalSeconds
Configuring the Network
Changing the network profile from Public to Private:
Set-NetConnectionProfile -InterfaceAlias 'Wi-Fi' -NetworkCategory 'Private'
Removing Widgets
Disabling the Widgets application (and the annoying News Feed accompanying it):
winget uninstall "Windows Web Experience Pack"
Using the Clipboard
Pasting the clipboard, and extracting the first element on each row, separated by whitespace:
Get-Clipboard | ForEach-Object { $(-Split $_)[0] }To learn more, see The array sub-expression operator
Downloading Files
Invoke-WebRequest -Uri 'https://URL.ext' -OutFile 'FILE.ext'
Extract Archive
Expand-Archive -LiteralPath 'ARCHIVE.zip' -DestinationPath 'FOLDER'
Computing Checksums
Get-FileHash -Algorithm 'SHA256' -Path 'FILE'
Clear Recycling Bin
TIL There is a Clear-RecycleBin module. How neat!
Clear all recycling bins on every drive on the local machine without prompting for confirmation:
Clear-RecycleBin -Force