How-to: Save and Restore Windows Printer Connections

The three scripts below allow you to save the current users Printer Connections (Print Mappings) to a text file, and later to restore those same Printer Connections by reading the text file. The scripts also save and restore the users choice of default printer.

This can be useful if you need to delete the users profile (reprofile) or delete and re-create a user account, or to copy Printer Connections between user accounts. (The text file is stored in %HOMESHARE% rather than %USERPROFILE% because reprofiling will normally delete everything in %USERPROFILE%.)

[PrintSave.cmd]

cscript PrintQueues.vbs >"%HOMESHARE%\myprinters.txt"

[PrintQueues.vbs]

Option Explicit
On Error Resume Next

Dim strDefaultPrinter,objWMIService,colInstalledPrinters, objPrinter

' 1. Get the name of the default Printer        ###
strDefaultPrinter = DefaultPrinter

' 2. WMI query for current printer connections  ###

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

' 3. Main Loop through printer collection       ###

For Each objPrinter in colInstalledPrinters
   If Left(objPrinter.Name, 2) = "\\" Then 'Work only on network printers
      'Echo the printer name
      If strDefaultPrinter = objPrinter.Name Then
        Wscript.Echo objPrinter.Name & " DEFAULT"
      Else Wscript.Echo objPrinter.Name
      End If

   End If 'End of check for network printers
Next 'End of the loop through the printer collection

'-----------------
' Functions
'-----------------
'Return the default printer
Function DefaultPrinter
    Dim strComputer
    Dim Result
    
    strComputer = "."
    Result = ""
    
    Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colInstalledPrinters = objWMIService.ExecQuery _
     ("Select * from Win32_Printer")
    For Each objPrinter in colInstalledPrinters
        If objPrinter.Default = True Then
         Result = objPrinter.Name
        End If
    Next
    DefaultPrinter = Result
End Function

[PrintRestore.cmd]

@echo off
Setlocal
:: Restore printer drive mappings from myprinters.txt

set _vbs=%temp%\RestorePrinterConnections.vbs
Echo On Error resume Next > %_vbs%
Echo Dim WshNetwork >> %_vbs%
Echo Set WshNetwork = CreateObject("WScript.Network") >> %_vbs%

for /f "usebackq tokens=1,2" %%G in ("%HOMESHARE%\myprinters.txt") do call :map %%G %%H
cscript %_vbs%
Del %_vbs%
goto:eof

:map
Echo %1
Echo WshNetwork.AddWindowsPrinterConnection "%1" >> %_vbs%
if /i "%2"=="Default" echo WshNetwork.SetDefaultPrinter "%1" >> %_vbs%

Examples

List the printer connections for the current user:

C:\> cscript PrintQueues.vbs

Save all network printer connections to the file %HOMESHARE%\myprinters.txt

C:\> PrintSave

Install a set of printer connections by reading the file %HOMESHARE%\myprinters.txt

C:\> PrintRestore

“Do you know the difference between education and experience? Education is when you read the fine print; experience is what you get when you don’t” ~ Pete Seeger

Related VBScript commands

qchange.vbs - Re-assign network printer connections.
PRINTBRM - Print queue Backup/Recovery.


 
Copyright © 1999-2024 SS64.com
Some rights reserved