Switching users print servers with Powershell
So we recently setup a new Win2k8 R2 print server to replace our aging 2k3 server, and I needed a way to easily switch a whole bunch of users over to the printer mappings on the new server. After some digging, I came up with this method of switching everyone over at login with Powershell:
<#
.SYNOPSIS
This is a Powershell script to remove the old <printserver> printers and replace them with Printers mapped to <newprintserver>
.DESCRIPTION
This script will run at logon, and accomplish the following tasks:
1) Sets ErrorAction to Silently Continue to hide any red errors if a printer connection doesn’t exist
2) Gets the machines list of installed printers and passes it to the $localprinters variable
3) Searches the $localprinters variable for the printer names and declares them
4) Tests each variable for existence, and if it exists removes the old <printserver> version and installs the <newprintserver> version
5) Checks what the original default printer was, and if it was a <printserver> one changes it to the new <newprintserver> version
.NOTES
This script is fairly easy to modify and tailor to a different site by modifying the what is searched for via -pattern, and adjusting the printer connections in each test case.
.LINK
http://www.blueglowy.com
#>
$ErrorActionPreference = "SilentlyContinue"
$localprinters = Get-WmiObject -Class Win32_Printer -ComputerName ${env:computername}
$defaultprt = Get-WMIObject -class Win32_Printer -computer ${env:computername} -Filter Default=True | Select Name,Default
$test = Select-String -inputObject $localprinters -pattern "<printer1>" -quiet
$test2 = Select-String -inputObject $localprinters -pattern "<printer2>" -quiet
$test3 = Select-String -inputObject $localprinters -pattern "<printer3>" -quiet
$test4 = Select-String -inputObject $localprinters -pattern "<printer4>" -quiet
$prt1 = Select-String -inputObject $defaultprt -pattern "<printer1>" -quiet
$prt2 = Select-String -inputObject $defaultprt -pattern "<printer2>" -quiet
$prt3 = Select-String -inputObject $defaultprt -pattern "<printer3>" -quiet
$prt4 = Select-String -inputObject $defaultprt -pattern "<printer4>" -quiet
if ($test -eq "True") {(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\<printserver>\<printer1>")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\<newprintserver>\<printer1>")}
else {""}
if ($test2 -eq "True") {(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\<printserver>\<printer2>")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\<newprintserver>\<printer2>")}
else {""}
if ($test3 -eq "True") {(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\<printserver>\<printer3>")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\<newprintserver>\<printer3>")}
else {""}
if ($test4 -eq "True") {(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\<printserver>\<printer4>")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\<newprintserver>\<printer4> PCL5")}
else {""}
#Check for default printer and reset it to users original choice if printer was removed
if ($prt1 -eq "True") {($Printer = Get-WmiObject win32_printer | where {$_.name -match "<printer1>"})}
Elseif ($prt2 -eq "True") {($Printer = Get-WmiObject win32_printer | where {$_.name -match "<printer2>"})}
Elseif ($prt3 -eq "True") {($Printer = Get-WmiObject win32_printer | where {$_.name -match "<printer3>"})}
Elseif ($prt4 -eq "True") {($Printer = Get-WmiObject win32_printer | where {$_.name -match "<printer4> PCL5"})}
Else {$Printer = ""}
$Printer.SetDefaultPrinter()
Printer Check Complete
