r/PowerShell • u/MartianMoon • Jan 13 '20
Network Printer Driver Nuker - Deletes all network printers & their drivers, then remaps the printers
https://github.com/Justin-Lund/Printer-Remapper7
u/Dsraa Jan 14 '20
Not bad.
I wrote something similar this past year to delete and remap network probers because I had too many users having printer issues frequently, and then bombarding the help desk with calls for simple nonsense. It basically detected and filter against your ip addressr or ou location and then query the local print server for that user and looped through and installed all printers for that location. Worked wonders for the help desk.
5
u/MartianMoon Jan 14 '20
Any chance you'd like to share the code? I'd love to take a look at that =D
3
u/Dsraa Jan 14 '20
Why Shure! I wanted to perfect it up a little more, as its a bit rough around the edges but I'll share the relevant snippet of powershell. There's a few ways to pare the results to get the desired result, so I'm sure this can probably be modified/perfected to fit your scenario:
$PrintSRV = Get-ADObject -LDAPFilter "(&(&(&(uncName=*)(objectCategory=printQueue))))" -properties *|Sort-Object -Unique -Property shortservername #| select distinguishedname, shortservername
$PrintSRV.Distinguishedname.Split(',')[-3]
$PCOU = Get-ADComputer "$env:ComputerName" -Properties *
$PCOU.Distinguishedname.Split(',')[-3]
foreach($srv in $PrintSRV){
if( $PCOU.Distinguishedname.Split(',')[-3] -like $SRV.Distinguishedname.Split(',')[-3]){
write-output "Server: $srv.shortServerName.split('=)[-3]"
$Prnlistbox = $srv | select shortservername, location
$Prnlistbox
[void] $listBox.Items.Add("Print Server:$srv.shortServerName.split('=')[1]")
}
#Else{Write-Output "No Match"}
}
Hope this gives some ideas.
→ More replies (3)2
5
u/williamt31 Jan 14 '20
Had one of the weirdest support tickets I've ever seen couple weeks back, wish I could've tried this.
Printer guy recreated queue and everyone but 1 user could re-install it.
If I logged into her desktop I could install it, on my laptop profile and when she logged into my laptop both could install.
I re-created her profile on her desktop and she still couldn't install.
Kept rotating thru 3 diff errors, couple hex codes and 'Out of memory'. No clue what broke but ended up having to re-image the whole thing to resolve.
1
u/MartianMoon Jan 14 '20
That's bizarre, I wonder if this would've fixed it!
3
u/williamt31 Jan 14 '20
Getting tired, I forgot to mention I even dis-joined/re-joined the machine to domain just to make sure it re-applied policies, deleted driver files, clearing spool32, even tried copying driver files from my pc at one point. yeah, super weird.
5
→ More replies (1)2
u/Dsraa Jan 15 '20
Edited.....
Now that I think about it, it sounds like a registry profile issue. The printer in question probably had a corrupted registry key under her profile that needed clearing.... That would be my guess, probably could do a WMI query and c clear it that way.
→ More replies (1)→ More replies (2)1
u/DudeImMacGyver Jan 14 '20 edited Nov 10 '24
edge public wine seemly whole caption many quaint muddle arrest
This post was mass deleted and anonymized with Redact
4
3
u/OlivTheFrog Jan 14 '20
Hi martianMoon,
Excellent, but why using WMI and not CIM ? Have you some restrictions due to old Installed PS Versions on the wokstations (PS 2.0) ?
Another point : your script
Gather all info for later use
unmap network map,
restart spooler,
delete drivers,
remap network printers (then install updated drivers).
restore user default printer
...for all printers.
This script could be an inspiration source for some other scripts like the following ideas :
In a far past, I'm using something equivalent to troubleshoot one or more specific drivers used by one or more network printers). This script ran as a logon script and to avoid to annoy users more than necessary, put a flag file on the user computer after process, and check if the flag file is present before process.
Another idea could be migrate Network printers from a print server to another one. Same idea : Logon script, inventory Network Printer on computer, If \\Server1\Printer1 then do something like your script, and map \\server2\Printerx
Regards
Olivier
3
u/MartianMoon Jan 14 '20 edited Jan 14 '20
Yeah, I made this to be compatible with machines as old as XP (obviously nobody should be using XP at this point, let alone W7, but that's the reasoning)
Feel free to make a fork and flip WMI to CIM & just scrap the lines for XP :)
3
2
u/rratt Jan 14 '20
Minor suggestions, parameters to delete printers and drivers, so it is not done by default. Also, test for localadmin before trying to delete drivers otherwise it gets errors.
4
u/MartianMoon Jan 14 '20
I've had no problems running this without local admin, might be due to restrictions in your environment
The whole point of this is to delete the printers & drivers so I'm not sure why I'd make that a parameter? If it wasn't done by default and you opted not to do it then it would literally do nothing
2
u/yardshop Jan 14 '20
Just curious why you have it call a VBScript to do the deleting which itself just uses WMI to do the work.
Otherwise it looks very useful and I'm going to give it a try here.
2
u/MartianMoon Jan 14 '20
To be honest, that portion is copy/pasted out of another script. I had no idea that script uses WMI to do the work, I'll look into that! Do you happen to know how to go about deleting the drivers with WMI?
3
u/yardshop Jan 14 '20
That VBScript will give you the examples you need. Basically it makes a WMI call to get an object for a particular printer driver, then calls the Delete method on it. I imagine it will be somewhat similar to your Get-WMIObject calls above that part that stop and start the spooler.
2
3
u/Dsraa Jan 15 '20
Probably would be 'get-wmiobject' and then pipe $_.delete at the end should work. Id have to look up the exact syntax for printer objects to be sure.
2
u/TheF-inest Jan 14 '20
Haven't read into it but is this only for printers installed from and print server?
2
u/MartianMoon Jan 14 '20
Yes, but you could easily modify it to include non-network printers if you'd like to
3
Jan 14 '20
Man, I made something like this a while back but it was so janky. This is just so much better. Thanks for the share
2
u/jrodsf Jan 14 '20
Nice. Much cleaner than the one I wrote years ago to perform migrations to a new print server.
I'll have to revisit mine and see if I can't improve it a bit. I think I was reading the printer mappings out of the user hive (ugh) because I hadn't learned much wmi yet at the time.
Thanks for sharing!
2
Jan 14 '20 edited Jan 14 '20
probably something like this on windows 10
# Grab all printers that are network
$Printers = [System.Collections.Generic.List[object]]::new()
$storePrinters = (get-printer | where-object {$_.type -match "Connection"})
$Printers.Add($storePrinters)
# If you want to save the configs to a file
$Printers | ConvertTo-Json | out-file -FilePath "$home\desktop\printerconfigs.json"
# Remove all printers that are network
$storePrinters | Remove-Printer -PassThru | Remove-PrinterDriver
# Add them back
Foreach ($Printer in $Printers){
write-output "Adding $($printer.name)"
Add-Printer -ConnectionName $Printer.name
}0
Jan 14 '20
[deleted]
2
Jan 14 '20 edited Jun 07 '25
[removed] — view removed comment
3
u/unquietwiki Jan 14 '20
I still upvoted it. But I'm dismayed that we're stuck maintaining something MS may decide to nuke any day because of the next Russian hacker / feature replacement. Deleted my original complaint.
1
15
u/roerius Jan 14 '20
Looks great! Thank you for sharing!
What use cases have you had for this/what problems have you had it resolve?