Listen to this Post
In the realm of IT infrastructure and support, PowerShell scripting is an essential skill for managing Windows Server environments. This article delves into a collection of PowerShell scripts tailored for Windows Server 2019, providing practical examples and commands to streamline your administrative tasks.
You Should Know:
PowerShell is a powerful scripting language and command-line shell designed for system administration. Below are some verified scripts and commands that can be used to manage and troubleshoot Windows Server 2019 effectively.
1. Automating User Account Creation
Creating multiple user accounts manually can be time-consuming. Use the following PowerShell script to automate the process:
<h1>Script to create multiple user accounts</h1>
$Users = Import-Csv -Path "C:\UserList.csv"
foreach ($User in $Users) {
New-ADUser -Name $User.Name -GivenName $User.FirstName -Surname $User.LastName -SamAccountName $User.SamAccountName -UserPrincipalName $User.UPN -AccountPassword (ConvertTo-SecureString $User.Password -AsPlainText -Force) -Enabled $true
}
2. Monitoring Disk Space
Keeping track of disk space is crucial for server maintenance. Use this script to generate a report:
<h1>Script to monitor disk space</h1>
Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID, @{Name="Size(GB)";Expression={[math]::round($<em>.Size/1GB,2)}}, @{Name="FreeSpace(GB)";Expression={[math]::round($</em>.FreeSpace/1GB,2)}}
3. Managing Services
To stop, start, or restart services remotely, use the following commands:
<h1>Stop a service</h1> Stop-Service -Name "ServiceName" -Force <h1>Start a service</h1> Start-Service -Name "ServiceName" <h1>Restart a service</h1> Restart-Service -Name "ServiceName"
4. Configuring Firewall Rules
Managing firewall rules is essential for security. Use this script to add a new rule:
<h1>Add a new firewall rule</h1> New-NetFirewallRule -DisplayName "Allow Web Traffic" -Direction Inbound -Protocol TCP -LocalPort 80,443 -Action Allow
5. Backup and Restore
Automate backups using PowerShell:
<h1>Backup a directory</h1> Compress-Archive -Path "C:\ImportantData" -DestinationPath "D:\Backup\ImportantData.zip" <h1>Restore a directory</h1> Expand-Archive -Path "D:\Backup\ImportantData.zip" -DestinationPath "C:\RestoredData"
What Undercode Say:
PowerShell scripting is a game-changer for Windows Server administrators. By leveraging these scripts, you can automate repetitive tasks, enhance productivity, and ensure the smooth operation of your server environment. Here are additional commands to expand your toolkit:
- Check System Information:
Get-WmiObject -Class Win32_ComputerSystem
-
List Installed Software:
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version
-
Manage Active Directory:
Get-ADUser -Filter * | Select-Object Name, SamAccountName
-
Schedule Tasks:
New-ScheduledTaskAction -Execute "C:\Scripts\Backup.ps1"
-
Network Configuration:
Get-NetIPConfiguration
-
Event Logs:
Get-EventLog -LogName System -Newest 10
-
Remote Management:
Enter-PSSession -ComputerName "RemoteServer"
Expected Output:
By implementing these PowerShell scripts and commands, you can significantly improve the efficiency and reliability of your Windows Server 2019 environment. Whether you’re automating user management, monitoring system health, or configuring security settings, PowerShell provides the flexibility and power needed to meet your administrative goals.
References:
Reported By: Fabiano Meda – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



