PowerShell is a powerful automation and management tool for system administrators. It allows you to run scripts, control services, users, events, and interact with external systems and APIs. This guide will show you how to use PowerShell for real-world tasks, step by step.
Installation and setup
- Download PowerShell 7+ from the official website: https://github.com/PowerShell/PowerShell
- Open PowerShell or PowerShell ISE (the built-in script editor).
Allow scripts to run by executing:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
First script Hello.ps1
Create a file named Hello.ps1 and paste the following:
Write-Host "Hello, PowerShell automation!"
Get-Date
$env:USERNAME
Run the script with the command:
.\Hello.ps1
Syntax basics
PowerShell is based on a clear and concise structure:
- Commands: Get-Process, Start-Service, Stop-Computer
- Variables: $user = “Admin”
- Conditions: if ($x -gt 10) { … }
- Loops: foreach ($item in $list) { … }
Use the PowerShell ISE editor or Visual Studio Code with the PowerShell extension for highlighting and autocompletion.
Script style and structure
We recommend following these principles:
- Name functions using the verb-noun style: Get-Report, Start-Backup
- Use Try-Catch to handle errors
- Add comments and parameter descriptions (docblocks)
- Separate logic by functions and modules
Example: memory monitoring
param([int]$Threshold = 80)
$mem = Get-CimInstance Win32_OperatingSystem |
Select @{Name='FreeGB'; Expression={[math]::Round($_.FreePhysicalMemory/1MB,2)}}
if ($mem.FreeGB -lt $Threshold) {
Write-Warning "Memory low: $($mem.FreeGB) GB"
} else {
Write-Host "Memory OK: $($mem.FreeGB) GB"
}
The script checks whether there is enough free memory and displays a warning if there is not enough.
Task Scheduler
You can run PowerShell scripts on a schedule:
- Open Task Scheduler
- Create a new task → Action tab
Specify:
powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\Hello.ps1"
- Set up a trigger based on time or event
User management
Local users:
$pass = Read-Host -AsSecureString "Введите пароль"
New-LocalUser -Name "TestUser" -Password $pass -FullName "Тестовый пользователь"
Disable-LocalUser -Name "TestUser"
Remove-LocalUser -Name "TestUser"
Active Directory (with CSV file):
Import-Csv users.csv | ForEach-Object {
New-ADUser -Name $_.Name -SamAccountName $_.Login `
-AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -Force) -Enabled $true
}
Windows Event Monitoring
Get-WinEvent -LogName System -MaxEvents 10 |
Format-Table TimeCreated, Id, Message
Filtering:
Get-WinEvent -FilterHashtable @{LogName='Application'; Id=1000; StartTime=(Get-Date).AddDays(-1)}
Export to CSV:
... | Export-Csv "C:\Logs\app.csv" -NoTypeInformation
Backup and archiving
$src = "C:\Important"
$dst = "D:\Backup\Backup_$(Get-Date -Format 'yyyyMMdd').zip"
Compress-Archive -Path "$src\*" -DestinationPath $dst
Add-Content "D:\Backup\backup_log.txt" "Backup: $(Get-Date) → $dst"
This script creates an archive from a folder and logs the backup.
Email notifications
Send-MailMessage -From "admin@domain.com" -To "team@company.com" `
-Subject "Backup report" -Body "Backup completed successfully." `
-SmtpServer "smtp.domain.com" -Attachments "D:\Backup\*.zip"
Integration with CI/CD and the cloud
- You can use PowerShell in GitHub Actions, Azure Automation Runbooks, or on VPS SSD
Example for GitHub Actions:
- name: Run PowerShell script
run: pwsh ./monitor.ps1
- In Azure, you can run a script on a schedule with RBAC and execution logs.
PowerShell is not just a shell, but a universal automation tool. Real-world examples of backing up, deleting old files, and running programs help you quickly grasp the basics. Important: Always test scripts with the -WhatIf parameter or in a test environment to avoid unwanted changes.