# Powershell - Printers

# Printers

## Delete Printers

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

### Delete Network Printers

Look at printers

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

```powershell
Get-printer | where{$_.Network -eq 'true'}
```

Delete network printers

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

Delete printers except for the following

```powershell
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

```powershell
$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.

```powershell
#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

```powershell
# 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

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