r/PowerShell • • Jan 13 '20

Network Printer Driver Nuker - Deletes all network printers & their drivers, then remaps the printers

https://github.com/Justin-Lund/Printer-Remapper
221 Upvotes

47 comments sorted by

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?

16

u/MartianMoon Jan 14 '20

Thanks!
Use case: Basically, whenever a user says _________ printer function isn't doing what it's supposed to, or if the printer is spitting out random characters/symbols.

If any printer-specific features aren't working like 2-sided printing, hole punch, etc - anything you'd enable through the printer settings - this will typically fix it, since those are typically driver issues (assuming the issue is client-side, of course.)
I recently had a user unable to open their printer settings at all, for multiple printers, & this script fixed it =)

6

u/Texas_Ponies Jan 14 '20

Have not had a chance to fully review just saw the post.

To add to use case, I would assume you'd be able to run this as a startup script in GPO and target your machine/ user base.

My use case would be I am about to stand up a new PDC and move my network printer drivers (well rebuild with the new updated drivers) off a 2008 r2 about to be decom'd to my 2012. Once they are in place I'd enable my GPO and let the work take place. Then I'd go about my decom project.

Again have to look at it all but getting into this project now so this might be a great start. I don't want startup to keep nukeing and paving so might have to do some tweaking.

4

u/MartianMoon Jan 14 '20

Hope it helps with your project!

→ More replies (1)

18

u/inyobase Jan 14 '20

Looks like something to run if you're experiencing problems with network printers. And it's best to nuke the site from orbit, it's the only way to be sure...

4

u/len_sam Jan 14 '20

Would like to know this also.

1

u/spacewolfy Jan 14 '20

Interesting to see people solve this issue so many different ways!

My coworker and I created something similar to migrate from Windows 7 to Windows 10 in VDI with profile management.

We use a DEM logoff script in VDI to dump a raw list of the network printers from Windows 7 desktop sessions to the user's profile.

When logging into Windows 10 an async PowerShell script is triggered with DEM to run once and install the printers fresh from the print servers based on the printer list from W7 and then they're tracked with profile management/DEM moving forward.

7

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.

2

u/MartianMoon Jan 14 '20

Awesome, thank you!

→ More replies (3)

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

u/jrodsf Jan 14 '20

Definitely sounds like a nuke-it-from-orbit kinda situation.

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 (1)

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

→ More replies (2)

4

u/macgeek89 Jan 14 '20

awesome!! thanks for the share

4

u/MartianMoon Jan 14 '20

No worries! =)

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

u/furyoshonen Jan 14 '20

Your code is so well documented! Thank you.

2

u/MartianMoon Jan 14 '20

Comments are the most important part of coding!

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

u/MartianMoon Jan 14 '20

Alright cool, thanks!

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

u/[deleted] 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

u/[deleted] 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

u/[deleted] Jan 14 '20

[deleted]

2

u/[deleted] 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.