user2387281
user2387281

Reputation: 1

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

Upvotes: 0

Views: 8968

Answers (2)

lulu bui
lulu bui

Reputation: 1

updated answer using PowerShell on window 2022 using PrintManagement PS Module

Get-Printer | where Name -match $pattern | ForEach-Object{
    Set-Printer $_.Name -DriverName $driver -Confirm
}

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36277

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
}

Upvotes: 1

Related Questions