Windows PowerShell can be used to control printers from WSH using WMI and the WScript.Network COM object. When demonstrating the performance of individual tasks, both types of tools will be used. This article shows how to add or remove network printer
1. Getting a list of printer connections:- It is easiest to get a list of printers installed on your computer using the Win32_Printer WMI class: Get-WmiObject -Class Win32_Printer -ComputerName You can also create a list of printers using the WScript.Network COM object, which is commonly used in WSH scripts: (New-Object -ComObject WScript.Network).EnumPrinterConnections() This command returns a simple collection of strings of names of ports and printer devices without distinguishing marks, which makes interpretation difficult.
2. Adding a network printer:- To add a new network printer, use the WScript.Network object: (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Printserver01\Xerox5")
3. Setting the default printer:- To set up a default printer using the WMI service, locate the printer in the Win32_Printer collection and call the SetDefaultPrinter method: (Get-WmiObject -ComputerName . -Class Win32_Printer -Filter "Name='HP LaserJet 5Si'").SetDefaultPrinter() It is somewhat easier to work with a WScript.Network object, which has a SetDefaultPrinter method that takes only the printer name as an argument: (New-Object -ComObject WScript.Network).SetDefaultPrinter('HP LaserJet 5Si')
4. Removing the connection to the printer:- To remove a printer connection, use the WScript.Network RemovePrinterConnection method: (New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Printserver01\Xerox5")
Also consider using Action1 to add or remove network printer if:
- You need to perform this action on multiple (hundreds or even thousands) computers simultaneously.
- Some of your endpoints are laptops not connected to corporate network at all times.