PowerShell + .
NET Framework Cheat Sheet
1. Basic .NET Usage
Check PowerShell version & loaded assemblies:
$PSVersionTable
[AppDomain]::CurrentDomain.GetAssemblies()
Create an object from a .NET class:
$webClient = New-Object System.Net.WebClient
2. Date & Time
Current date & time:
[DateTime]::Now
Custom format:
[DateTime]::Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")
Add days:
(Get-Date).AddDays(10)
3. String & Encoding
Convert to Base64:
$text = "PowerShell"
[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($text))
Decode Base64:
[System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("UG93ZXJTaGVsbA=="))
Uppercase:
"powershell".ToUpper()
4. Random & Security
Generate a GUID:
[guid]::NewGuid()
Generate a random password:
Add-Type -AssemblyName System.Web
[System.Web.Security.Membership]::GeneratePassword(12,2)
Compute SHA256 Hash:
$text = "SecureData"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
$hash = [System.Security.Cryptography.SHA256]::Create().ComputeHash($bytes)
[BitConverter]::ToString($hash) -replace "-", ""
5. Networking
Download a file:
$url = "https://example.com/file.zip"
$output = "C:\Temp\file.zip"
(New-Object System.Net.WebClient).DownloadFile($url, $output)
Get external IP:
Invoke-RestMethod -Uri "https://api.ipify.org?format=json"
Ping:
$ping = New-Object System.Net.NetworkInformation.Ping
$ping.Send("google.com")
6. GUI & User Interaction
Message Box:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("Hello!", "PowerShell")
Open File Picker:
Add-Type -AssemblyName System.Windows.Forms
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.ShowDialog()
Save File Dialog:
Add-Type -AssemblyName System.Windows.Forms
$save = New-Object System.Windows.Forms.SaveFileDialog
$save.Filter = "Text Files (*.txt)|*.txt"
$save.ShowDialog()
7. Audio & Speech
Play sound:
Add-Type -AssemblyName System.Media
$sound = New-Object System.Media.SoundPlayer "C:\Windows\Media\notify.wav"
$sound.PlaySync()
Text-to-Speech:
Add-Type -AssemblyName System.Speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$speak.Speak("Hello from PowerShell!")
8. File Operations
Read text file:
[System.IO.File]::ReadAllText("C:\Temp\file.txt")
Write text file:
[System.IO.File]::WriteAllText("C:\Temp\file.txt", "Hello .NET")
Copy file:
[System.IO.File]::Copy("C:\Temp\file.txt","C:\Temp\file_copy.txt",$true)
9. Performance & Diagnostics
Stopwatch:
$timer = [System.Diagnostics.Stopwatch]::StartNew()
Start-Sleep -Seconds 2
$timer.Stop()
$timer.Elapsed.TotalSeconds
Get processes:
[System.Diagnostics.Process]::GetProcesses() | Select-Object ProcessName, Id
10. Miscellaneous Fun
Beep sound:
[console]::Beep(1000,500)
Progress bar:
for ($i=1; $i -le 100; $i++) {
Write-Progress -Activity "Loading" -Status "$i% Complete" -PercentComplete $i
Start-Sleep -Milliseconds 50
}
List fonts:
(Get-ChildItem "C:\Windows\Fonts").Name