共用方式為


Use PowerShell in Windows 8 to Remove Printers

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell 3.0 in Windows 8 to remove printers.

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I have been talking to various people from the Charlotte Windows PowerShell User Group all week about doing another Windows PowerShell Saturday. It is an awful lot of work, but I think we are going to do this again. The Windows PowerShell Saturday in Charlotte sold out within a few days, and there have been many positive comments about the event. That means that people found it to be a valuable experience. So we will have another Windows PowerShell Saturday. (By the way, if you want to have one where you live, let us know via scripter@microsoft.com.)

To remove a printer with Windows PowerShell, I use the Remove-Printer function from the PrinterManagement module. There are two ways to use the Remove-Printer function:

Remove-Printer [-Name] <String> [-AsJob [<SwitchParameter>]] [-CimSession

<CimSession>] [-ComputerName <String>] [-PassThru [<SwitchParameter>]]

[-ThrottleLimit <Int32>] [-Confirm [<SwitchParameter>]] [-WhatIf

[<SwitchParameter>]] [<CommonParameters>]

 

Remove-Printer [-AsJob [<SwitchParameter>]] [-CimSession <CimSession>]

[-PassThru [<SwitchParameter>]] [-ThrottleLimit <Int32>] -InputObject

<CimInstance> [-Confirm [<SwitchParameter>]] [-WhatIf [<SwitchParameter>]]

[<CommonParameters>]

What this means is that if I type the exact printer name, I can use the Remove-Printer function directly. It also tells me that I can pipe a printer object to the function. By pipelining a printer object, I can use wildcard characters.

Begin with Get-Printer

I usually begin things by using a Get type of command. So the first thing I do is use the Get-Printer function to see what printers are defined. The command is shown here:

Get-Printer

The command and its associated output are shown here:

Image of command output

I can use a wildcard character to avoid typing a complete printer name as shown here:

PS C:\> Get-Printer | where name -Like "my*"

 

Name                           ComputerName    Type         DriverName

----                           ------------    ----         ----------

myotherlaser                                   Local        Brother Laser Leg Typ...

Or, I can type the exact printer name and supply it directly to the –Name parameter as shown here:

PS C:\> Get-Printer -Name myotherlaser

 

Name                           ComputerName    Type         DriverName

----                           ------------    ----         ----------

myotherlaser                                   Local        Brother Laser Leg Typ...

Both of these commands return printer objects, and therefore, they can be piped to the Remove-Printer function. This is shown here:

Get-Printer -Name myotherlaser | Remove-Printer

Get-Printer | where name -like "my*" | Remove-Printer

Remember Whatif

Of course, before I add a Remove-Printer object, I want to use the –Whatif switch to ensure that I am doing exactly what I want to do. Here is an example of a near disaster:

PS C:\> Get-Printer | where name -match "my*" | Remove-Printer -WhatIf

What if: Deleting printer Microsoft XPS Document Writer

What if: Deleting printer \\dc1.iammred.net\HP LaserJet 2100 PCL6

What if: Deleting printer myotherlaser

PS C:\>

Luckily, I used –Whatif, so I did not delete a bunch of my printers.

Directly remove a printer

I can use the Remove-Printer function directly to remove a printer if I know the exact name. If I am unsure of the printer name, I use the Get-Printer function to list my printers, and I copy and paste the name. With quick edit mode turned on, I can highlight the printer name with my mouse, press ENTER to copy it to the clipboard, and then right-click to paste it. This is shown in the image that follows.

Image of command output

Here is the command:

Remove-Printer -Name myotherlaser

After I have deleted the printer, I may decide to delete the printer driver and the printer port (if necessary). To do that, I use the following functions:

PS C:\> Get-PrinterDriver -Name "Brother*"

 

Name                                PrinterEnvironment MajorVersion    Manufacturer

----                                ------------------ ------------    ------------

Brother Laser Leg Type1 Class Dr... Windows x64        4               Brother

 

PS C:\> Get-PrinterDriver -Name "Brother*" | Remove-PrinterDriver

I use the Get-PrinterPort function, and I decide that I do not need to remove any printer ports.

That is all there is to using Windows PowerShell to remove printers. This also concludes Printer Week. Join me tomorrow when I will talk about running scripts on remote file shares.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy