PowerShell
Getting Help
Functions
File Management
Create an empty file file.txt
# Alias: "ni"
New-Item 'file.txt'
Create a copy of file old.txt located at new.txt:
# Alias: "cp"
Copy-Item 'old.txt' 'new.txt'
Create a symbolic link to a file target.txt located at link.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'
Clipboard Manipulation
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")
Replace the component of the current directory corresponding to the home
directory with ~:
$(Get-Location).ToString().Replace($HOME, "~")
Create a new alias vi to invoke the NeoVim editor:
New-Alias -Name "vi" -Value "nvim.exe" -Description "NeoVim" -Option ReadOnly
Test if a network connection can be formed with a remote host on a particular port:
Test-NetConnection `
-ComputerName example.com `
-CommonTCPPort SMB
Open 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
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
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'
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!