PowerShell Scripting
Nicholas Mutsaerts
System Administrator | Microsoft 365 Specialist | IT Support Specialist | Technical Writer | Linux Enthusiast
PowerShell scripting has become an essential tool for help desk professionals and system administrators, enabling them to automate tasks, improve efficiency, and manage systems more effectively. With its command-line interface and scripting capabilities, PowerShell allows IT teams to execute complex administrative functions with minimal manual effort. From automating repetitive tasks, such as user account management and software installations to retrieving system information and troubleshooting issues, PowerShell significantly enhances productivity.
As part of a brief series on PowerShell scripting, several commonly used PowerShell scripts are being explored. For further insights, please refer to previous articles, Using PowerShell for Microsoft and Removing Bloatware from Windows 11.
Here are three common PowerShell scripts:
Note: Be sue to save the scripts as a .ps1 file and save them in an open folder, such as desktop or download folders.
Script 1: Create a Local Administrator Account
# Define Variables
$Username = "LocalAdmin"
$Password = ConvertTo-SecureString "YourSecurePassword123!" -AsPlainText -Force
$FullName = "Local Administrator"
$Description = "This is a local administrator account."
# Create the Local Admin Account
New-LocalUser -Name $Username -Password $Password -FullName $FullName -Description $Description
# Add the User to the Administrators Group
Add-LocalGroupMember -Group "Administrators" -Member $Username
Write-Host "Local Administrator Account '$Username' has been created successfully." -ForegroundColor Green
Script 2: Create a Local Standard User Account
# Define Variables
$Username = "LocalUser"
$Password = ConvertTo-SecureString "YourSecurePassword123!" -AsPlainText -Force
$FullName = "Local Standard User"
$Description = "This is a local standard user account."
# Create the Local User Account
New-LocalUser -Name $Username -Password $Password -FullName $FullName -Description $Description
# Add the User to the "Users" Group (Default Group)
Add-LocalGroupMember -Group "Users" -Member $Username
Write-Host "Local User Account '$Username' has been created successfully." -ForegroundColor Cyan
Notes:
Remove-LocalUser -Name "Username"
Script 3: Add Network Printer
# Define Variables
$PrinterName = "Office_Printer"
$PrinterIP = "192.168.1.100"
$DriverName = "Generic / Text Only" # Change this to match your printer driver
$PortName = "IP_$PrinterIP"
# Check if the Port Exists, If Not, Create It
if (-not (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue)) {
Add-PrinterPort -Name $PortName -PrinterHostAddress $PrinterIP
Write-Host "Created Printer Port: $PortName" -ForegroundColor Green
} else {
Write-Host "Printer Port Already Exists: $PortName" -ForegroundColor Yellow
}
# Check if the Printer Exists, If Not, Add It
if (-not (Get-Printer -Name $PrinterName -ErrorAction SilentlyContinue)) {
Add-Printer -Name $PrinterName -PortName $PortName -DriverName $DriverName
Write-Host "Added Printer: $PrinterName" -ForegroundColor Green
} else {
Write-Host "Printer Already Exists: $PrinterName" -ForegroundColor Yellow
}
Write-Host "Network Printer Setup Completed!" -ForegroundColor Cyan
How to use this Script
Additional Commands
Get-Printer
Get-PrinterDriver
Remove-Printer -Name "Office_Printer"
Script 4: Map a Network Drive
This script allows you to specify a drive letter, network path, and optional credentials if needed.
$DriveLetter = "Z:"
$NetworkPath = "\\server-name\shared-folder"
# Map the network drive
New-PSDrive -Name $DriveLetter.Trim(':') -PSProvider FileSystem -Root $NetworkPath -Persist
How it use this script:
Script 5: Map a Network Drive with Credential Prompt
If you need to enter a username and password manually, proceed to use this modify script.
$DriveLetter = "Z:"
$NetworkPath = "\\server-name\shared-folder"
$User = "DOMAIN\username"
$Password = Read-Host "Enter password" -AsSecureString
# Create a credential object
$Credential = New-Object System.Management.Automation.PSCredential($User, $Password)
# Map the network drive
New-PSDrive -Name $DriveLetter.Trim(':') -PSProvider FileSystem -Root $NetworkPath -Credential $Credential -Persist
This script will prompt user for a password securely and uses a credential object to authenticate.
Disclaimer
Use this script at your own discretion and responsibility. Running scripts comes with inherent risks. Prior to deploying scripts in a live environment, consider testing with virtual machines. It is highly recommend consulting your IT team for support before proceeding.