Skip to main content

Powershell - Printers

Printers

Delete Printers

https://community.spiceworks.com/topic/1932797-use-powershell-to-delete-a-mapped-printer

Delete Network Printers

Look at printers

Get-WmiObject -Class Win32_Printer | where{$_.Network -eq 'true'} 
Get-printer | where{$_.Network -eq 'true'}

Delete network printers

Get-WmiObject -Class Win32_Printer | where{$_.Network -eq 'true'} | foreach{$_.delete()}

Delete printers except for the following

Get-WmiObject -Class Win32_Printer | where{$_.name -notlike 'microsoft PDF'} | foreach{$_.delete()}

You can also do from any server login as admin you need to keep the name of computer in text file

$computers= get-content c:\computers.txt
foreach ($computer in $computers) 
{Get-WmiObject -Class Win32_Printer -computername $computer | where{$_.name -notlike 'microsoft PDF'} | foreach{$_.delete()}}

or against all computers in Ad.

#ForEach ($COMPUTER in (GC c:\computers.txt))
ForEach ($COMPUTER in (Get-ADComputer -Filter {OperatingSystem -notLike "*Server*"} | Select -ExpandProperty Name))

{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer" -f red}
 
else {
  Get-WmiObject -Class Win32_Printer -computername $computer | where{$_.name -notlike 'microsoft PDF'}| foreach{$_.delete()}
     }
}

Delete network printers

# Create Function to Remove Printers
Function Delete-NetworkPrinters
{
    $NetworkPrinters = Get-WmiObject -Class Win32_Printer | Where-Object{$_.Network -and $_.SystemName -like "\\<servername>*" -or $_.Name -like "* on <servername>"}
    If ($NetworkPrinters -ne $null)
    {
      Try
      {
        Foreach($NetworkPrinter in $NetworkPrinters)
        {
	        $NetworkPrinter.Delete()
	        Write-Host "Successfully deleted the network printer:" + $NetworkPrinter.Name -ForegroundColor Green	
        }
      }
      Catch
      {
        Write-Host $_
      }
    }
    Else
    {
      Write-Warning "Cannot find network printer in the currently environment."
    }
}
# Remove Printers
Delete-NetworkPrinters

Delete

get-printer \\server\* | remove-printer