# encoding: ascii
# api: powershell
# title: Migrate-UserPrinters
# description: Used for migrating a users network printer connections from one server to another. Useful when migrating your printers to a new print server and need don’t want to make your users remap their printers manually. If deployed with SCCM make sure to set the package to run as the user and not SYSTEM.
# version: 0.1
# type: class
# author: Tony Sathre
# license: CC0
# x-poshcode-id: 6064
# x-archived: 2016-10-30T02:15:59
# x-published: 2016-10-22T19:45:00
#
#
param (
    [Parameter(Mandatory=$true)]
        [string]$OldPrintServer,
    [Parameter(Mandatory=$true)]
        [string]$NewPrintServer
)

#$ErrorActionPreference = 'SilentlyContinue'
$Printers = Get-WmiObject -Class Win32_Printer | where { $_.Network -eq $true }

foreach ($Printer in $Printers) {
    if ($Printer.SystemName -eq "\\$OldPrintServer") {            
        $ShareName = $Printer.ShareName
        $Name = $Printer.Name -replace $OldPrintServer, $NewPrintServer
        (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\$NewPrintServer\$ShareName") | Out-Null

        if ($Printer.Default -eq $true) {
            (Get-WmiObject -Class Win32_Printer | where { $_.Name -eq $Name}).SetDefaultPrinter() | Out-Null
        }

        $Printer.Delete() | Out-Null
    }
}
