Cleaning up Windows Server printers

September 15, 2023

Hue Truong

Today, I had to write some PowerShell scripts to clear out printer queues on a Windows Server 2022. I created these scripts with a -WhatIf command to test their functionality without actually executing them. If I were to use them as intended, I would remove the -WhatIf command.

Get all printers and delete all of them

$printers = Get-Printer

foreach ($printer in $printers) {
    Write-Output "Printer Name: $($printer.Name)"

    # Delete printer
    try {
        Remove-Printer -Name $printer.Name -WhatIf
        Write-Output "Would delete printer: $($printer.Name)"
    } catch {
        Write-Output "Failed to delete printer: $($printer.Name)"
    }
}

I wouldn’t recommend running this script as it deletes all printer queues on the server. The PowerShell code below uses a regex pattern to remove printers that begin with “PATTERN”.

Get all printers and delete printers matching pattern

$printers = Get-Printer

# Define the regex pattern for "PATTERN" followed by any characters
$pattern = '^PATTERN.*'

foreach ($printer in $printers) {
    # Check if the printer name matches the pattern
    if ($printer.Name -match $pattern) {
        Write-Output "Printer Name: $($printer.Name)"

        # Delete printer
        try {
            Remove-Printer -Name $printer.Name -WhatIf
            Write-Output "Would delete printer: $($printer.Name)"
        } catch {
            Write-Output "Failed to delete printer: $($printer.Name)"
        }
    }
}

Next, I can use the PowerShell script below to remove the standard TCP/IP ports.

Get all printer ports and delete the standard TCP/IP ports

# Get all printer ports
$ports = Get-PrinterPort

# Define the regex pattern for an IP address followed by an optional underscore and two digits
$pattern = '^([0-9]{1,3}\.){3}[0-9]{1,3}(_[0-9]{2})?'

foreach ($port in $ports) {
    # Check if the port name matches the IP address pattern
    if ($port.Name -match $pattern) {
        Write-Output "Port Name: $($port.Name)"

        # Delete port
        try {
            Remove-PrinterPort -Name $port.Name -WhatIf
            Write-Output "Would delete port: $($port.Name)"
        } catch {
            Write-Output "Failed to delete port: $($port.Name)"
        }
    }
}

Finally, I exported a list of printer drivers so that I can manually removed printers I want to keep.

Get printers driver names and save to printerdrivers.csv

Get-PrinterDriver | select Name | Export-Csv printerdrivers.csv -NoTypeInformation

After making changes to printerdrivers.csv, execute the PowerShell script below to uninstall printer drivers.

Remove printer drivers from printerdrivers.csv

# Import the CSV file
$printerDrivers = Import-Csv -Path 'printerdrivers.csv'

# Loop through each printer driver in the CSV file
foreach ($driver in $printerDrivers) {
    # Try to remove the printer driver
    try {
        Remove-PrinterDriver -Name $driver.Name -WhatIf -ErrorAction Stop
        Write-Output "Would have removed printer driver: $($driver.Name)"
    }
    catch {
        Write-Output "Failed to remove printer driver: $($driver.Name)"
    }
}

That’s all there is to it.

join the conversation

Leave the first comment

Avatar for Hue Truong

Hue Truong

IT Professional, Systems Engineer, and Cloud Administrator. Passionate about exploring the latest technologies and solving complex problems.

Related Posts

More articles like this