Migrate printers to new server

If you move printers from one server to another the users needs to reconnect all printers…

And… of course it’s easy to do with a small Powershell script. 🙂

Function Migrate-Printer {
    PARAM (
        [string] $ShareName,
        [string] $oldServer,
        [string] $newServer
    )

    $currentPrinter = Get-WmiObject -Query "SELECT * FROM Win32_Printer WHERE Network=True AND ShareName = '$($ShareName)' AND SystemName = '\\\\$($oldServer)'"

    if ($currentPrinter -eq $null) {
        Write-Verbose "Cant find \\$($oldServer)\$($ShareName)"
    } else {
        Write-Verbose "Migrating printer $($ShareName) from $($oldServer) to $($newServer)"

        $net = New-Object -com WScript.Network

        Write-Verbose "Adding printer \\$($newServer)\$($ShareName)"
        $net.AddWindowsPrinterConnection("\\$($newServer)\$($ShareName)")

        Write-Verbose "Removing printer \\$($newServer)\$($ShareName)"
        $net.RemovePrinterConnection("\\$($oldServer)\$($ShareName)")
    

        if ($currentPrinter.Default -eq "True") {
            Write-Verbose "Setting default to \\$($newServer)\$($ShareName)"
            $net.SetDefaultPrinter("\\$($newServer)\$($ShareName)")
        }
    }

}

Example usage

Migrate-Printer -ShareName "PRINTER001" -oldServer "OLDSERVER" -newServer "NEWSERVER"

Comments are closed.