Avatar of Rich Smith
Rich Smith
Flag for United Kingdom of Great Britain and Northern Ireland asked on

PowerShell to remove printers

Hi,
 
I have just upgraded my old printer server from 2008r2 (TCPRINT01) to 2019 (TCPRINT02), I need to remove all the printers mapper to the old print server from my Windows 10 workstations, I have been trying to find a PowerShell script that can do this but I can only find a script that will remove all printers, can anyone help with a script that will only remove printers connected to TCPRINT01?


I was going to run the script at login unless anyone can recommend a different way to do this?
 
 
 

PowershellPrinters and Scanners

Avatar of undefined
Last Comment
DEMAN-BARCELO (MVP) Thierry

8/22/2022 - Mon
DEMAN-BARCELO (MVP) Thierry

Hi Rich,

you have many kind of script that are doing this migration from server/printer to another server/printer:

https://github.com/carygarvin/Migrate_PrintQs.vbs/blob/master/Migrate_PrintQs.vbs

These scripts can  (simply) replace the old printer/server with a new printer during the logon of the user.


Here was my sample script :

https://base.faqexchange.info/TOUTE%20LA%20DOCUMENTATION%20EST%20ICI/Scripting/VbScript/movprinter.vbs.txt

The format file was "SourceServer,SourcePrinter,TargetServer,TargetPrinter" using NETBIOS names.
Only users that were connected to the old shares are modified.
Rich Smith

ASKER
Hi Thierry,

Thank you for the links, I don't have much experience with .vbs scripts, would these be run manually on each workstation or using a GPO to run at logon?

Thank you,
Rich
DEMAN-BARCELO (MVP) Thierry

The script could be run for testing purpose in a connected user profile.

Connected printers are defined in HKCU (current user registry), so the administrator cannot change their configuration.

So, ideally, Yes, the script must be run in the logon script or in a GPO starting after the login of the user.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
oBdA

Here's a bit of PowerShell; it's in test mode and will not delete any connection; remove the -WhatIf at the end to run it for real:
$printServer = 'TCPRINT01'
Get-Printer | Where-Object {($_.Type -eq 'Connection') -and ($_.ComputerName -match "$($printServer)\b")} | Remove-Printer -Verbose -WhatIf

Open in new window

Rich Smith

ASKER
Hi oBdA,

I tried running the script and it only returns local printers and not the network printers, do I need to change the 'connection' ?

thank you, Rich
oBdA

Then, sorry, but the only explanation I have is that you're not running the exact script I posted.
Local printers will have a type of "Local" and therefore will not pass the "($_.Type -eq 'Connection')" test, and neither will local printers have the property ComputerName set, so they won't pass "($_.ComputerName -match "$($printServer)\b")", either.
Demo run with some local printers (no network printers on this machine) that don't make it through the "Where-Object":
PS C:\> Get-Printer -Name 'Microsoft*' | select Name, Type, ComputerName

Name                           Type ComputerName
----                           ---- ------------
Microsoft XPS Document Writer Local
Microsoft Print to PDF        Local


PS C:\> Get-Printer -Name 'Microsoft*' | Where-Object {($_.Type -eq 'Connection')}
PS C:\>
PS C:\>

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
DEMAN-BARCELO (MVP) Thierry

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.