Mapping Printers via OU or Computer Name

By | April 12, 2013

A simple script that will map printers for users based on either the computer name or OU the computer is in, very handy if users roam between computers.

Create the following script, save it in an accessible share such as netlogon and set it to run as a user startup script via GPO.

'==================================================
'PrinterMapping.vbs
'This scripts removes all existing mapped printers
'It then queries AD and gets computer objusct OU
'It then maps printers via OU or computer name
'================================================

Set objSysInfo = CreateObject("ADSystemInfo")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
Set cNetCommands = WScript.CreateObject("WScript.Network")
'===================================
'Remove Mapped printers
'===================================

strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
'WScript.Sleep 5000
'======================================================
'Get computer AD path
'======================================================
strComputer = objSysInfo.ComputerName
'======================================================
'Get Computer name
'======================================================
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'WScript.Echo "Computer Name: " & strComputerName
'======================================================
'Get OU of computer
'======================================================
Set objComputer = GetObject("LDAP://" & strComputer)
arrOUs = Split(objComputer.Parent, ",")
arrMainOU = Split(arrOUs(0), "=")
'Wscript.Echo arrMainOU(1)
'======================================================
'Map Printers to all computers and then set default
'======================================================
cNetCommands.AddWindowsPrinterConnection "\serverprinter"
cNetCommands.SetDefaultPrinter "\serverprinter"
'======================================================
'Map Printers via OU, line require for each OU. Use "SetPrinter" for default and "SetPrinternotdefault" for non default printer, which ever default comes last in the script will be the default on the computer.
'======================================================

if InStr(1,arrMainOU(1),"IT",vbTextCompare) > 0 then call SetPrinter("\serverprinter1")

if InStr(1,arrMainOU(1),"IT",vbTextCompare) > 0 then call SetPrinternotdefault("\serverprinter2")

'======================================================
'Map Printers via Computer name, line required for each computer name, you may also use part of the name i.e. s-room1-p would map printer to all computers with "s-room1-p" in there names. Use "SetPrinter" for default and "SetPrinternotdefault" for non default printer, which ever default comes last in the script will be the default on the computer.'=====================================================

if InStr(1,strComputerName,"s-it",vbTextCompare) > 0 then call SetPrinternotdefault("\serverprinter1")
if InStr(1,strComputerName,"s-it",vbTextCompare) > 0 then call SetPrinter("\serverprinter2")

'======================================================<

'Clear Memory and quit
'=====================================================
Set objWMIService = Nothing
Set colInstalledPrinters = Nothing
Set objComputer = Nothing
Set objSysInfo = Nothing
Set wshShell = Nothing
Set cNetCommands = Nothing
wscript.quit

'======================================================
'This subroutine installs and sets the default printer
'======================================================
Sub SetPrinter(ByVal PrinterPath)
DIM WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.AddWindowsPrinterConnection(PrinterPath)
WshNetwork.SetDefaultPrinter Printerpath
'Result = WshShell.Popup("Sucessfully mapped" & vbCrLf & PrinterPath, 2, "Printer Mapped", 0 + 64)
end sub

'======================================================
'This subroutine installs the printer
'======================================================
Sub SetPrinternotdefault(ByVal PrinterPath)
DIM WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.AddWindowsPrinterConnection(PrinterPath)
'Result = WshShell.Popup("Sucessfully mapped" & vbCrLf & PrinterPath, 2, "Printer Mapped", 0 + 64)
end sub

8 thoughts on “Mapping Printers via OU or Computer Name

  1. Pingback: 手機殼

  2. Pingback: SEO

  3. Daniel Beaulieu

    I think this is a real great blog article.Really looking forward to read more. Want more.

  4. doudoune moncler

    I definitely wanted to write down a brief message to be able to appreciate you for all the nice solutions you are giving out on this website. My time-consuming internet research has at the end been rewarded with sensible details to share with my company. I ‘d suppose that most of us visitors actually are unequivocally fortunate to be in a fabulous place with so many wonderful people with very beneficial ideas. I feel quite happy to have used your web page and look forward to plenty of more enjoyable times reading here. Thanks a lot once again for a lot of things.
    doudoune moncler

  5. Bill

    Heya i’m for the first time here. I found this board and I find It really helpful & it helped me out a lot.
    I hope to present one thing back and aid others such as you helped me.

  6. Mcgread

    Super-Duper website! I am loving it!! Will come back again. I am bookmarking your feeds also

Comments are closed.