Updating default printers with Powershell

The problem: Due to issues with network printing on Windows Server 2012 R2, I decided to move from network printers to local printers on each of my client’s Remote Desktop servers. To make the transition easier for users, I needed to find a way to change the users’ default printers automatically to the local printers.

The solution: Powershell! These days it seems my go-to solution is scripting something in Powershell to do the hard work for me.

The logon script below will check the user’s local printer to see if it is a network printer. If it is, it will change the default setting in registry to the corresponding local printer.

# This script will check to see if the user's default printer is a network printer on the specified print server,
# then change the default to the corresponding local printer. It has been tested on a Windows Server 2012 R2 environment only.
# This script will only work if the following conditions are true:
# 1. There is only one print server in use
# 2. There are local printers configured on all of the RDS servers in the farm with the exact same names as the printers that are configured on the print server

# Replace the below variable with the name of your print server (it is FP01 in this example)

$printserver = "FP01"

# Checks the user's default printer

$default = Get-ItemProperty "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows"

# If the default printer is a network printer

If ($default.Device -like "\\$printserver\*")
{
# Cut off the server portion, then the last 15 characters to isolate the printer name
$printername = $default.Device.Substring($printserver.Length+3)
$printername = $printername.Substring(0,$printername.Length-15)

# Get the port details for the printer from HKCU and cut off the last 6 characters of the string (gives something like winspool,Ne09:)
$port = Get-ItemProperty "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
$port = $port.$printername
$port = $port.Substring(0,$port.Length-6)

# Combine printer name and port to create the new default printer string, then set in registry
$newdefault = "$printername,$port"
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows" -Name "Device" -Value $newdefault
}

exit

Note: This has been tested only on Windows Server 2012 R2. It will only work if you have one print server in use, and you have configured local printers on your RDS server with names matching the shared printers. This script is provided as-is with no guarantees of any kind.

Leave a Reply

Your email address will not be published. Required fields are marked *