Example PowerShell Scripts for Common Administrative Tasks
By FoxLearn 10/1/2024 3:21:08 AM 78
Get System Information
This script helps you retrieve basic system information.
# GetSystemInfo.ps1 Get-ComputerInfo | Select-Object CsName, OsArchitecture, WindowsVersion, WindowsBuildLabEx
Check Disk Space
This script helps you check disk space on all drives.
# CheckDiskSpace.ps1 Get-PSDrive -PSProvider FileSystem | Select-Object Name, @{Name="Used(GB)";Expression={[math]::round($_.Used/1GB,2)}}, @{Name="Free(GB)";Expression={[math]::round($_.Free/1GB,2)}}, @{Name="Total(GB)";Expression={[math]::round($_.Used/1GB,2) + [math]::round($_.Free/1GB,2)}}
List Installed Software
This script helps you list all installed software on the system.
# ListInstalledSoftware.ps1 Get-WmiObject -Class Win32_Product | Select-Object Name, Version
Create a New User
This script helps you create a new local user account.
# CreateUser.ps1 $Username = "NewUser" $Password = ConvertTo-SecureString "P@ssword123" -AsPlainText -Force New-LocalUser -Name $Username -Password $Password -FullName "New User" -Description "Created by script"
Backup a Folder
This script helps you copy a directory to a backup location.
# BackupDirectory.ps1 $source = "C:\Source" $destination = "D:\Backup\" Copy-Item -Path $source -Destination $destination -Recurse -Force
Check Network Connectivity
This script helps you ping a list of websites to check connectivity.
# CheckNetworkConnectivity.ps1 $sites = @("google.com", "microsoft.com", "stackoverflow.com") foreach ($site in $sites) { Test-Connection -ComputerName $site -Count 2 | Select-Object Address, ResponseTime }
Export Event Logs
This script helps you export the application event log to a CSV file.
# ExportEventLogs.ps1 Get-WinEvent -LogName Application | Export-Csv -Path "C:\Logs\ApplicationLogs.csv" -NoTypeInformation
Disable a User Account
This script helps you disable a specified local user account.
# DisableUser.ps1 $Username = "UserToDisable" Disable-LocalUser -Name $Username
Monitor CPU Usage
This script help you check CPU usage and sends an alert if it exceeds a threshold.
# MonitorCPUUsage.ps1 $threshold = 80 $cpuUsage = Get-Counter '\Processor(_Total)\% Processor Time' if ($cpuUsage.CounterSamples.CookedValue -gt $threshold) { Write-Host "Alert: CPU usage is above $threshold%!" }
Update Windows
This script helps you trigger a Windows Update.
# UpdateWindows.ps1 Install-Module PSWindowsUpdate -Force -SkipPublisherCheck Import-Module PSWindowsUpdate Get-WindowsUpdate -AcceptAll -Install
Install Software Updates
To update software or system components automatically, you can use this script.
Install-WindowsUpdate -AcceptAll -AutoReboot
Send User Notifications
To send reminders about upcoming events to an email distribution list, you can use this script.
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test" -Body "This is a test message !" -SmtpServer "smtp.gmail.com"
Perform Security Scans
To automatically run security scans to detect malware or vulnerabilities, you can use this script.
Start-MpScan -ScanType QuickScan
Clean Up Temporary Files
This script helps you remove temporary files from the specified directories
Get-ChildItem -Path C:\Windows\Temp\*, $env:TEMP\* -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Restart a Service
To restart a service, such as Microsoft SQL Server service, you can use commands like the following.
Restart-Service -Name MSSQLSERVER
- How to display GUI Message Boxes in PowerShell
- How to sign a powershell script
- How to run powershell script using task scheduler
- How to run powershell script from cmd
- How to run powershell commands in C#
- How to execute PowerShell script in C#
- How to delete a Windows service in PowerShell
- How to download file from url in PowerShell