Windows PowerShell isn’t just a command-line shell—it’s a full-blown scripting powerhouse that, when utilized right, can automate a great many routine (and even complex) administrative tasks. Whether you’re cleaning up a clogged system, scanning for aging log files, or kicking off monitoring alerts before something breaks, PowerShell has got you covered. It’s part of the modern Technology Stack that seasoned system administrators and tech-savvy professionals rely on daily.
But like a chainsaw in a novice’s hands, PowerShell demands respect. It’s elegant, efficient, and incredibly versatile—but with great power comes great responsibility. Still, for those with a tech-savvy background, or even curious learners hungry to seize the chance to master automation, this tool has an immense potential to be a game-changer. Below are some handy PowerShell code snippets every admin or power user should have in their toolbox to spice up their daily workflows.
Find the 10 Largest Files on a Drive
Running low on space and not sure where it went? This script quickly lists the biggest culprits eating up disk space on any drive.
$drive = "C:\"
Get-ChildItem -Path $drive -Recurse -File -ErrorAction SilentlyContinue |
Sort-Object Length -Descending |
Select-Object FullName, @{Name="SizeMB";Expression={"{0:N2}" -f ($_.Length / 1MB)}} -First 10
Find and Delete Files Older Than 30 Days
Perfect for clearing out old backups or logs in a directory (like C:\Logs). This one does the spring-cleaning for you.
$path = "C:\Logs"
$days = 30
Get-ChildItem -Path $path -Recurse -File | Where-Object {
$_.LastWriteTime -lt (Get-Date).AddDays(-$days)
} | Remove-Item -Force -Verbose
Send Email Alert If Disk Space is Low
Don’t wait for the “out of space” pop-up. Be proactive by getting email alerts when your disk space is running low.
$drives = Get-PSDrive -PSProvider FileSystem
foreach ($drive in $drives) {
if ($drive.Free -lt 10GB) {
Send-MailMessage -To "admin@example.com" -From "ps-alert@example.com" `
-Subject "Low Disk Space on $($drive.Name)" `
-Body "Only $([math]::Round($drive.Free/1GB,2)) GB left!" `
-SmtpServer "smtp.example.com"
}
}
Clear Temp Files and Recycle Bin
Run this script regularly to free up space and keep your system clutter-free. It removes temporary files and empties the Recycle Bin without asking.
Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
Write-Host "Temp and Recycle Bin cleaned."
Get Your Public IP Address
Need to quickly find your public-facing IP? This one-liner does it effortlessly.
(Invoke-WebRequest -Uri "http://ifconfig.me/ip").Content
Kill All Chrome Tabs Older Than 30 Minutes
Sometimes Chrome tabs go rogue. This script kills all Chrome processes older than 30 minutes to reclaim RAM. Pretty handy, isn’t it?
Get-Process chrome | Where-Object { $_.StartTime -lt (Get-Date).AddMinutes(-30) } | Stop-Process
Check Active Directory Users Whose Passwords Will Expire Soon
Useful in enterprise environments to stay ahead of user complaints. Get a list of users whose passwords will expire in the next 15 days.
Import-Module ActiveDirectory
Search-ADAccount -UsersOnly -PasswordExpiring -TimeSpan "15.00:00:00" |
Select-Object Name, @{Name="ExpiresIn";Expression={$_.PasswordNeverExpires -eq $false}}
Get Installed Applications with Version and Publisher
Need a list of installed software for auditing or cleanup? This script fetches installed apps with version and publisher details.
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher |
Sort-Object DisplayName
Check SSL Certificate Expiry of a Website
Avoid downtime by checking when your site’s SSL certificate is due to expire.
$domain = "https://example.com"
$tcp = New-Object Net.Sockets.TcpClient
$tcp.Connect($domain.Replace("https://", ""), 443)
$ssl = New-Object Net.Security.SslStream($tcp.GetStream(), $false, ({ $true }))
$ssl.AuthenticateAsClient($domain)
$cert = $ssl.RemoteCertificate
$certExpiry = ([datetime]::Parse($cert.GetExpirationDatestring()))
Write-Host "SSL certificate expires on: $certExpiry"
If you’re curious to learn more about Windows PowerShell techniques, following articles will prove to be more helpful:
PowerShell Technique: Perfect Email Address Validation
PowerShell Tricks – Basic XML Parsing
PowerShell Technique – Get RSS Feed For Weather Update
Windows PowerShell – Basic Constructs
Windows PowerShell Technique: Obtain Network Data
Windows PowerShell Technique: Get CPU Information
Windows PowerShell Technique: Get Hard Drive Information