PowerShell Scripting

PowerShell Scripting

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:

  1. Create a Local Administrator Account
  2. Create a Local Standard User Account
  3. Add a Network Printer
  4. Map a Network Drive
  5. Map a Network Drive with Credential Prompt

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:

  • These scripts should be run as an administrator in PowerShell.
  • Replace "YourSecurePassword123!" with a strong password.
  • If the account already exists, it will return an error.
  • To delete a user, use:

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

  1. Open PowerShell as Administrator.
  2. Modify $PrinterName, $PrinterIP, and $DriverName as needed.
  3. Run the script to install the printer.

Additional Commands

  • List installed printers:

Get-Printer        

  • List available printer drivers

Get-PrinterDriver        

  • Remove a printer

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:

  • Replace "Z:" with your preferred drive letter.
  • Replace "\\server-name\shared-folder" with the actual network path.
  • The -Persist flag ensures the drive remains mapped after a reboot.


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.

要查看或添加评论,请登录

Nicholas Mutsaerts的更多文章

  • Removing Bloatware from Windows 11

    Removing Bloatware from Windows 11

    Even though Windows is the dominant operating system, Microsoft known for its nefarious activities by pre-installing…

  • The Future of Internet Addressing and When Disabling IPv6 Makes Sense

    The Future of Internet Addressing and When Disabling IPv6 Makes Sense

    IPv6 is paving the way for the future of internet addressing, offering a significantly larger address space and…

  • Steam Gaming on Linux

    Steam Gaming on Linux

    Gaming on Linux has evolved dramatically in recent years. Steam has been on of the pivotal in making it easier to enjoy…

  • Using Netsh and Ipconfig Commands for Effective Network Troubleshooting

    Using Netsh and Ipconfig Commands for Effective Network Troubleshooting

    For IT help desks and system administrators, mastering Netsh and Ipconfig commands is key to effective network…

    1 条评论
  • Windows Package Manager (WINGET)

    Windows Package Manager (WINGET)

    Microsoft has introduced a new feature called Windows Package Manager, commonly referred to as winget. This…

  • Using Netsh Commands to Resolve Network Issues on Windows

    Using Netsh Commands to Resolve Network Issues on Windows

    Netsh is a versatile command-line utility designed for managing, configuring, and troubleshooting local or remote…

  • Rocky Linux

    Rocky Linux

    Rocky Linux was developed, in large part, due to Red Hat's decision to discontinue CentOS Linux. The first release…

  • Unlocking the Magic of VirtualBox

    Unlocking the Magic of VirtualBox

    Explore the magic behind VirtualBox. This powerful hypervisor can transform your computing experience by letting you…

  • Knowledge Base for new hires and current staff

    Knowledge Base for new hires and current staff

    An essential part of IT onboarding of new hires and current staff lies with providing quick reference guides. As…

  • Manjaro

    Manjaro

    Manjaro is a versatile and user-friendly Linux operating system that delivers an excellent out-of-the-box experience…