Converting the Windows Script Host SetDefaultPrinter Method

Definition: Assigns a remote printer the role Default Printer.

SetDefaultPrinter

You know, you’re right: life would be awfully dull if you always kept the same default printer, wouldn’t it? Fortunately you can use WMI and the Get-WMIObject cmdlet to change the default printer on a computer. The following block of code uses Get-WMIObject – and the –query parameter – to retrieve a collection of all the printers on the computer named Test Printer. (Because printer names must be unique on a computer, there will only be one item in the collection.) Once the printer has been returned the second line of code uses the SetDefaultPrinter method to make Test Printer the default printer on the computer:

$a = Get-WMIObject -query "Select * From Win32_Printer Where Name = 'Test Printer'"
$a.SetDefaultPrinter()

Unlike Windows Script Host, PowerShell – and WMI – can be used to assign a default printer on a remote computer. To do that, just add the –computerName parameter followed by the name of the remote machine:

$a = Get-WMIObject -computerName atl-ws-001 -query "Select * From Win32_Printer Where Name = 'Test Printer'"
$a.SetDefaultPrinter()

See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page