PowerShell Automation for System Administrators: 10 Practical Scripts That Save Time
PowerShell
Automation for System Administrators: 10 Practical Scripts That Save Time
Introduction
Managing Windows servers
manually can quickly become time-consuming as your infrastructure grows. From
checking disk space to creating user accounts and monitoring services,
repetitive tasks can consume valuable time that could be spent on strategic
initiatives.
PowerShell is Microsoft’s
powerful automation and scripting language that enables System Administrators
to automate routine tasks, improve consistency, and reduce human error. This
article explores 10 practical PowerShell scripts that every Windows System
Administrator should know.
Why Use PowerShell?
PowerShell offers several advantages:
·
Automates repetitive
administrative tasks
·
Reduces manual errors
·
Manages local and remote
systems
·
Integrates with Active
Directory, Microsoft 365, Azure, and Windows Server
·
Saves significant time in daily
operations
Script 1 – Check Disk
Space
Display available disk space on all local drives.
Get-PSDrive -PSProvider
FileSystem |
Select-Object Name,
@{Name="Free(GB)";Expression={[math]::Round($_.Free/1GB,2)}},
@{Name="Used(GB)";Expression={[math]::Round(($_.Used)/1GB,2)}}
Use Case: Daily health checks and
storage capacity planning.
Script 2
– List Services That Are Not Running
Identify stopped services quickly.
Get-Service |
Where-Object {$_.Status -eq "Stopped"} |
Sort-Object DisplayName
Use Case: Troubleshooting application
and server issues.
Script 3 – Check
System Uptime
Determine how long a server has been running.
(Get-CimInstance
Win32_OperatingSystem).LastBootUpTime
Use Case: Maintenance scheduling and
reboot verification.
Script 4 –
Retrieve Event Log Errors
Display recent critical system errors.
Get-WinEvent -LogName System
-MaxEvents 20 |
Where-Object {$_.LevelDisplayName -eq "Error"}
Use Case: Daily server health reviews.
Script 5
– List Disabled Active Directory Users
Run this from a system with the Active Directory PowerShell module
installed.
Search-ADAccount -AccountDisabled
-UsersOnly
Use Case: Account audits and cleanup.
Script 6
– Find Computers That Haven’t Logged On Recently
Identify inactive computer accounts.
Get-ADComputer -Filter * -Properties LastLogonDate |
Sort-Object LastLogonDate
Use Case: Removing stale devices from
Active Directory.
Script 7 –
Restart a Remote Service
Restart a service on a remote server.
Restart-Service -Name Spooler
For remote administration, combine this with PowerShell Remoting (Invoke-Command) or other remote
management tools as appropriate.
Use Case: Restarting print spoolers or
application services.
Script 8 – Check
Windows Updates
View installed Windows updates.
Get-HotFix |
Sort-Object InstalledOn -Descending
Use Case: Security compliance and patch
verification.
Script 9 – Export
Local Users
Export local user information to a CSV file.
Get-LocalUser |
Export-Csv C:\Reports\LocalUsers.csv -NoTypeInformation
Use Case: User audits and documentation.
Script 10
– Check Top CPU-Consuming Processes
Identify processes consuming the most CPU resources.
Get-Process |
Sort-Object CPU -Descending |
Select-Object -First 10
Use Case: Performance troubleshooting.
Best
Practices for PowerShell Automation
·
Test scripts in a
non-production environment first.
·
Use descriptive comments within
scripts.
·
Store scripts in a
version-controlled repository.
·
Run PowerShell with the least
privileges necessary.
·
Sign scripts where
organizational policy requires it.
·
Schedule recurring tasks using
Task Scheduler or other approved automation tools.
·
Log script output for
troubleshooting and auditing.
Common Mistakes to Avoid
|
Mistake |
Recommendation |
|
Running untested scripts in production |
Validate scripts in a test environment first |
|
Hardcoding passwords |
Use secure credential management methods |
|
Ignoring error handling |
Implement try/catch blocks where appropriate |
|
Not logging script activity |
Write logs for auditing and troubleshooting |
|
Running everything as Administrator |
Follow the principle of least privilege |
Conclusion
PowerShell
is one of the most valuable tools in a Windows System Administrator’s toolkit.
By automating repetitive tasks such as health checks, Active Directory
management, service monitoring, and reporting, administrators can improve
efficiency, reduce errors, and manage larger environments with confidence.
Start
by automating one or two daily tasks, then gradually build a library of
reusable scripts tailored to your organization’s needs.
Meta Description
Discover 10 practical
PowerShell scripts every Windows System Administrator should know. Learn how to
automate server administration, Active Directory tasks, health checks,
reporting, and performance monitoring.
Tags
·
PowerShell
·
Windows Server
·
System Administrator
·
Automation
·
Active Directory
Comments
Post a Comment