powershellwindows-server-2008wmipowershell-2.0windows-server-2003

PowerShell - Delete a print queue from a print server


I have a printer server which hosts around 1000 printers. I want to delete one of these.

I am currently using this command:

$p = Get-WmiObject -Class Win32_printer -ComputerName $server -namespace "root\CIMV2" -filter "name='printer0456'"

It works, it finds the printer, and then I can use this command to delete it:

$p.delete()

But what freaks the hell out of me, is that it takes like 2-3 minutes for the first command to find the printer. To me, it makes to sense at all.

Is there something I am doing wrong, or something else I can use to delete a printer faster ?

EDIT: check my response

That way, it only fetch until it founds the correct printer, instead of parsing the whole printer list.


Solution

  • TEMPORARY SOLUTION I HAVE FOUND

    $p = $null
    Get-WmiObject -Class Win32_printer -ComputerName $server|ForEach-Object{
        if($_.name -eq "printer0456"){
            $p = $_
            break
        }
    }
    if($p -ne $null){
        $p.delete()
    }