Top Ad unit 728 × 90

Latest Update

recent

How to Automate Windows Admin Tasks with PowerShell (A Beginner's Guide)

How to Automate Windows Admin Tasks with PowerShell (A Beginner's Guide)

The Problem: Same Task, Every Day

If you've ever manually created the same folder structure for a new project, exported a list of installed software for an audit, or checked which machines on your network still need a Windows update you already have a use case for PowerShell. You're just doing it the slow way.

PowerShell is the scripting language built into every copy of Windows. It's been there since Windows 7, and yet a huge number of IT professionals have never written a single script. This guide is for those people.

What Is PowerShell, Exactly?

PowerShell is a command-line shell and scripting language built by Microsoft. Unlike the old Command Prompt, PowerShell works with objects structured data you can sort, filter, and pipe between commands. That's what makes it genuinely useful for IT work, not just running the occasional ipconfig.

Two versions exist: Windows PowerShell 5.1 (built into Windows 10/11) and PowerShell 7.x (the newer cross-platform version). For most everyday Windows admin tasks, 5.1 works fine. For new projects or anything cross-platform, download PowerShell 7 from Microsoft's GitHub releases page.

Opening PowerShell the Right Way

Right-click the Start button and choose "Windows PowerShell (Admin)" or "Terminal (Admin)." Running as Administrator matters tasks that touch services, user accounts, and system settings all require elevated privileges.

If you're on Windows 11, Windows Terminal is the better interface. It supports multiple tabs and handles PowerShell 7 cleanly. Worth installing if you haven't already.

Your First Useful Command

Before scripts, learn cmdlets. A cmdlet (pronounced "command-let") follows a consistent Verb-Noun structure:

Get-Process        # Lists running processes
Get-Service        # Shows services and their status
Get-ChildItem      # Lists files in a folder (like ls or dir)

Try this right now: open PowerShell and run:

Get-Service | Where-Object {$_.Status -eq "Stopped"}

You'll get every stopped service on your machine in one line, no clicking through the Services console. That's the idea.

4 Scripts You'll Actually Use

1. Find Files Modified in the Last 7 Days

Get-ChildItem -Path "C:\Users\YourName\Documents" -Recurse |
Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) } |
Select-Object Name, LastWriteTime

Change the path to any folder. Great for auditing recent changes or tracking down what broke after a Friday deployment.

2. Export a List of Installed Software

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher |
Export-Csv -Path "C:\installed-software.csv" -NoTypeInformation

Run this on any machine and you get a clean CSV of installed apps perfect for audits, comparisons, or pre-upgrade documentation.

3. Check Disk Space on All Drives

Get-PSDrive -PSProvider FileSystem |
Select-Object Name,
  @{N="Free(GB)";E={[math]::Round($_.Free/1GB,2)}},
    @{N="Used(GB)";E={[math]::Round($_.Used/1GB,2)}}

One command, all drives, formatted cleanly. No more clicking through File Explorer to check space on each partition.

4. Restart a Service If It's Stopped

$service = Get-Service -Name "Spooler"
if ($service.Status -eq "Stopped") {
    Start-Service -Name "Spooler"
        Write-Output "Print Spooler was stopped. Restarted it."
        } else {
            Write-Output "Print Spooler is running fine."
            }

Replace Spooler with any service name. Drop this into a scheduled task to run every 15 minutes for a service that keeps falling over.

How to Save and Run Your Scripts

Write your script in Notepad or VS Code, save it with a .ps1 extension (e.g., check-service.ps1), then run it from PowerShell:

.\check-service.ps1

If you get an execution policy error, run this once in an admin session:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This allows locally-created scripts to run while keeping unsigned scripts from the internet blocked.

Common Mistakes to Avoid

Running without testing first. Always test scripts on dummy data or in a non-production environment. A loop that deletes or moves files won't ask twice.

No error handling. Wrap risky operations in try/catch blocks:

try {
    Remove-Item "C:\TempLogs" -Recurse -Force
    } catch {
        Write-Output "Failed to delete: $_"
        }

Hardcoding paths and usernames. Use variables instead it makes scripts reusable across different machines without editing the code each time.

No logging for unattended scripts. Add Start-Transcript -Path "C:\script-log.txt" at the top of any script that runs on a schedule. When something breaks at 3am, you'll be glad it's there.

Where to Go From Here

The best way to learn PowerShell is through real problems. Next time you find yourself doing something repetitive, search "how to do X in PowerShell" and adapt what you find. Start with one-liners, then save them as scripts, then run them on a schedule.

Microsoft's documentation at learn.microsoft.com is genuinely good and free. PowerShell 7 installs side-by-side with Windows PowerShell and doesn't replace it, so there's no risk in trying it.

The Bottom Line

You don't need to become a developer to get real value from PowerShell automation. Even five or six scripts targeting your most repetitive tasks can save hours each month. The examples above are real, working, and ready to copy. Start with one, adapt it to your environment, and build from there.

All Rights Reserved by Bikram Bhujel © 2019 - 2030
Powered By BikramBHUJEL, Designed by Bikram Bhujel
Powered by Blogger.