This is my PowerShwell translation of my colleague’s VBScript solution for mapping network printers with a script.
<# .SYNOPSIS Add a Network Printer connection, optionally making it the default printer. .DESCRIPTION Uses a COM object to add a Network Printer, and optionally sets that printer as the default. If an error is encountered, the exception is written to a file called Add-NetworkPrinter.err in the current $env:temp directory, and then the script terminates. This is my PowerShell translation of my colleague's VBScript solution: http://blog.uvm.edu/jgm/2014/06/11/parting-scripts-add-a-new-network-printer-and-set-it-as-default/ .PARAMETER PrinterShare The UNC path to the shared printer. e.g. \\printers1.campus.ad.uvm.edu\ETS-SAA-SamsungML-3560 .PARAMETER Default Specifies that the printer will also be set as the default printer for the current user. .EXAMPLE Add-NetworkPrinter.ps1 -PrinterShare '\\printers1.campus.ad.uvm.edu\ETS-SAA-SamsungML-3560' -Default .NOTES Script Name: Add-NetworkPrinter.ps1 Author : Geoff Duke <Geoffrey.Duke@uvm.edu> #> [cmdletbinding()] Param( [Parameter(Mandatory=$true, HelpMessage="Enter the UNC path to the network printer")] [ValidateNotNullOrEmpty()] [string] $PrinterShare, [parameter(Mandatory=$false)] [switch] $Default ) Set-PSDebug -Strict $PSDefaultParameterValues = @{"out-file:Encoding"="ASCII"} $ws_net = New-Object -COM WScript.Network write-verbose "Adding connection to $PrinterShare" try { $ws_net.AddWindowsPrinterConnection($PrinterShare) } catch { $error[0].exception | out-file (join-path $env:temp 'Add-NetworkPrinter.err') throw $error[0] } write-verbose "Setting the printer as the default" if ( $Default ) { try { $ws_net.SetDefaultPrinter($PrinterShare) } catch { $error[0].exception | out-file (join-path $env:temp 'Add-NetworkPrinter.err') throw $error[0] } } # the end
For use with Group Policy, it will probably be helpful to create a simple Set-DefaultPrinter.ps1 script. But that’s just the second stanza from the script above.
2 Comments