简体   繁体   中英

PowerShell Script to Change Printer Drivers on Several Printers

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
        }
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM