0

I'm trying to make a PowerShell script that will change all the drivers for a specific set of printers.

I have about 200 printers whose name begins with the letter Z. I also have a handful of printers that don't begin with the letter Z.

What i'm trying to accomplish is this... Any printer beginning with the letters ZEB has their driver changed to "HP LaserJet 4000 Series PS"

I've tried modifying the script below to work with what i need, but it just runs and nothing changes.

$driver = "HP LaserJet 4000 Series PS"
$pattern = 'ZEB'

$printers = gwmi win32_printer

foreach($printer in $printers){
        $name = $printer.name
        if($name -like $pattern){
                & rundll32 printui.dll PrintUIEntry /Xs /n $name DriverName $driver
        }
}
user2387281
  • 1
  • 1
  • 2

1 Answers1

1

This is fairly simple, as you already have half the stuff done from the comment response. I'm going to filter the printers that you want to modify as the loop is defined, so you only put the printers you want through the loop and the rest are skipped entirely. The main thing is the Where statement, which is working like your If statement to filter out just the right printers. It reads like this:

$Printers | Where{ $_.Name -like $pattern -and $_.DriverName -like '*HP LASERJET 4*' }

So it checks that the name starts with the letters ZEB, and checks that the drivers have 'HP LASERJET 4' somewhere in the driver name. All together it looks like this:

$driver = "HP LaserJet 4000 Series PS"
$pattern = 'ZEB*'

$printers = gwmi win32_printer

foreach($printer in ($printers|Where{$_.Name -like $pattern -and $_.DriverName -like '*HP LASERJET 4*'})){
        $name = $printer.name
        & rundll32 printui.dll PrintUIEntry /Xs /n $name DriverName $driver
}
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Awesome thanks! Instead of -like on the DriverName, can i do a -match for a more specific type of driver i want replaced? – user2387281 Mar 02 '17 at 21:08
  • @user2387281 `-match` would work, but I doubt you need it. If you know the exact driver, just take the `*`'s out of the `-like`. `-like` => wildcards; `-match` => regex. So if you wanted several drivers you can could use `-match` with the `|` operator rather than making multiple `-or` statements. – BenH Mar 02 '17 at 21:30
  • Cool, thanks for the info! I ended up using the -eq syntax so it was an exact match. I tried -match, but it would change all printers with HP LaserJet 4 – user2387281 Mar 02 '17 at 22:05