Closely related to my previous post, this simple script uses a WScript.Network COM object to set the default printer. The comment block is longer than the script, but I think it’s a useful little tool.
<#
.SYNOPSIS
Sets a Network Printer connection as the default printer.
.DESCRIPTION
Uses a COM object to sets the specified, installed printer as the default. If
an error is encountered, e.g., the specified printer isn't installed, the
exception is written to a file called Set-DefaultPrinter.err in the current
$env:temp directory, and then the script terminates, throwing the exception.
Based on 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
.EXAMPLE
Set-DefaultPrinter.ps1 -PrinterShare '\\printers1.campus.ad.uvm.edu\ETS-SAA-SamsungML-3560'
.NOTES
Script Name: Set-DefaultPrinter.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
)
Set-PSDebug -Strict
$PSDefaultParameterValues = @{"out-file:Encoding"="ASCII"}
$ws_net = New-Object -COM WScript.Network
try {
$ws_net.SetDefaultPrinter($PrinterShare)
}
catch {
$error[0].exception | out-file (join-path $env:temp 'Set-DefaultPrinter.err')
throw $error[0]
}
write-verbose "Default printer now $PrinterShare"