Converting the Windows Script Host AddWindowsPrinterConnection Method

Definition: Adds a Windows-based printer connection to your computer system.

AddWindowsPrinterConnection

At the moment Windows PowerShell has no cmdlets designed to help you manage printers. (Would that be a good thing for the PowerShell team to add? You bet it would.) However, you can use WMI to add a printer connection from within Windows PowerShell. This might not be the most intuitive bit of code you’ve ever seen, but it works:

$a = [wmiclass] Win32_Printer
$a.AddPrinterConnection("\\atl-ps-001\TestPrinter")

So what’s going on here? Well, in line 1 we’re using the [wmiclass] type adapter to connect to the Win32_Printer class. Note that we are connecting directly to the class itself; we are not trying to retrieve data from the class. (That is, we don’t want to get back individual instances of the Win32_Printer class. That’s because the method we’re going to use, AddPrinterConnection, is a “static” method, which means it works on the class as a whole rather than instances of the class.) After we make the connection we call AddPrinterConnection, passing AddPrinterConnection the path to the printer in question.

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