Converting the Windows Script Host AddWindowsPrinterConnection Method

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:

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.