PowerShell

It’s built on top of the .NET Framework, which means it’s not only smart but can also handle a whole lot of heavy lifting when it comes to system administration. Originally part of Windows, PowerShell has grown into a cross-platform tool (with PowerShell 7, also known as PowerShell Core) that runs on Windows, Linux, and macOS. Think of it as your own digital Swiss Army knife, ready to automate tasks, configure systems, or even chat up your disk drives if needed.

Using PowerShell is like having your own quirky digital butler – just type in a command and watch the magic happen! Its beauty lies in its simplicity and power: commands (or cmdlets) are designed to be readable and can seamlessly pass data from one command to the next through pipelines. Keeping PowerShell current is easy too: if you’re using Windows PowerShell 5.1, it comes with Windows, while PowerShell 7 can be updated via MSI installers, the Microsoft Store, or even using the WinGet package manager. Plus, PowerShell can be made secure through best practices like signing your scripts, running them with least privilege, and taking advantage of its robust security policies.

To give you a flavour of how it works, here are a couple of easy-to-read, intermediate scripts.

Firstly, to open PowerShell:

  1. Using the Start Menu Search:
    • Click the Start button or press the Windows key on your keyboard.
    • Type “PowerShell” in the search bar.
    • From the search results, click on “Windows PowerShell” (or “PowerShell” if you have the newer version installed).

(Tip: If you need administrative privileges, right-click the result and choose “Run as Administrator”—because sometimes PowerShell needs a little extra muscle.)

2. Using Windows Terminal:

  • Press Win + X or right-click the Start button to open the quick link menu.
  • Select “Windows Terminal” or “Windows Terminal (Admin)” if you need elevated rights.

*By default, Windows Terminal in Windows 11 may open with PowerShell as the active profile. If not, click the dropdown arrow in the Terminal window’s tab bar and choose “PowerShell”.

3. Using the Run Dialog:

  • Press Win + R to open the Run dialog.
  • Type powershell and hit Enter.

(For admin mode via Run, type powershell, then press Ctrl + Shift + Enter instead.)

The first script lists the top 5 processes based on CPU usage:

PowerShell
# Script 1: List the top 5 processes by CPU usage
Get-Process |
    Sort-Object CPU -Descending |
    Select-Object -First 5 |
    Format-Table ProcessName, CPU

And here’s a simple log rotation script that backs up a log file with a timestamp and then clears its contents, ensuring your log file stays fresh and manageable:

PowerShell
# Script 2: Simple log file rotation
$logPath = "C:\Logs\AppLog.txt"
$backupPath = "C:\Logs\Backup\AppLog_$(Get-Date -Format 'yyyyMMddHHmmss').txt"

if (Test-Path $logPath) {
    Copy-Item $logPath -Destination $backupPath
    Clear-Content $logPath
    Write-Output "Log rotated successfully: $backupPath"
} else {
    Write-Output "Log file not found: $logPath"
}

These examples showcase how PowerShell can be both fun and effective in handling everyday tasks.

—| MHL small logo |—
MyHomeLab

Photo: Augustine Wong