Powershell Script to remove printers and drivers I am learning Powershell Scripting as I move my work environment to Windows 10. The script below is to be edited by the SysAdmin (for the list of Print Queues and Printer Drivers) to specify what needs to be removed, then outputs what it did. I am looking for whether the Powershell syntax / convention is followed. This script will be distributed to other SysAdmins in my team when completed.

The script currently starts by declaring variables that will be used throughout the script. The variables tell the script which print queues to remove, and what print drivers to remove. It then loops through the arrays, removing the printers requested.

# Remove old printers by name - Written by Luke Barone
#
# VARIABLES TO EDIT
# $PrintersToRemove - (REQUIRED) An array of printers to cycle through and
#     remove. Use the exact name of the printer to have it included in the list
# $PrintDriversToRemove - (optional) An array of printer drivers to cycle
#     through and remove. You can obtain a list by running the "prndrvr.vbs -l"
#     command on a computer with drivers to remove. Alternatively, you can
#     leave the array empty, and the script will remove any unused (determined
#     by the system) print drivers automatically.
$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

# The variables below should only be changed from the default if the scripts
# are not in this location!
$PrintingAdminScriptsFolder = "C:\Windows\System32\Printing_Admin_Scripts\en-US"
$PrintScriptFiles = @("prnmngr.vbs", "prndrvr.vbs")

# SANITY CHECKS
If ($PrintersToRemove.Length -eq 0) {
    Write-Host "Required variable not filled! Please edit the script and re-run"
    Exit 1
}
If (($PrintScriptFiles | ForEach {Test-Path ($PrintingAdminScriptsFolder + "\" + $_)}) -contains $false) {
    Write-Host "Required scripts not found in specified folder! I need the two
    VBS scripts ($(PrintScriptFiles)) inside of $($PrintingAdminScriptsFolder)
    in order to function!"
    Exit 2
}

# SCRIPT START
Write-Host "Starting at $(Get-Date -Format g) on client $($env:COMPUTERNAME)"
C:\Windows\System32\cscript.exe //H:CSCRIPT //S | out-null
cd $PrintingAdminScriptsFolder
Write-Host ""
Write-Host "Removing selected printers..."
$PrintersToRemove | foreach {
    &amp; .\prnmngr.vbs -d -p "$_".ToString() | findstr "0x80041002" | out-null
    If ($LASTEXITCODE -eq 0) {
        Write-Host "    $_ does not exist"
    } else {
        Write-Host "    $_ removed"
    }
}
Write-Host ""

If($PrintDriversToRemove.count -gt 0) {
    Write-Host "Removing selected Printer Drivers..."
    $PrintDriversToRemove | foreach {
        &amp; .\prndrvr.vbs -d -m "$_" -v 3 -e "Windows x64".ToString() | findstr "0x80041002" | out-null
        If ($LASTEXITCODE -eq 0) {
            Write-Host "    $_ does not exist"
        } else {
            Write-Host "    $_ driver removed"
        }
    }
} else {
    Write-Host "Removing excess Printer Drivers..."
    .\prndrvr.vbs -x
}
Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available:

PS&gt; help printer

Name                              Category  Module                    Synopsis
----                              --------  ------                    --------
Add-Printer                       Function  PrintManagement           Adds a printer to the specified computer.
Add-PrinterDriver                 Function  PrintManagement           Installs a printer driver on the specified computer.
Add-PrinterPort                   Function  PrintManagement           Installs a printer port on the specified computer.
Get-Printer                       Function  PrintManagement           Retrieves a list of printers installed on a computer.
Get-PrinterDriver                 Function  PrintManagement           Retrieves the list of printer drivers installed on the specified computer.
Get-PrinterPort                   Function  PrintManagement           Retrieves a list of printer ports installed on the specified computer.
Get-PrinterProperty               Function  PrintManagement           Retrieves printer properties for the specified printer.
Read-PrinterNfcTag                Function  PrintManagement           Reads information about printers from an NFC tag.
Remove-Printer                    Function  PrintManagement           Removes a printer from the specified computer.
Remove-PrinterDriver              Function  PrintManagement           Deletes printer driver from the specified computer.
Remove-PrinterPort                Function  PrintManagement           Removes the specified printer port from the specified computer.
Rename-Printer                    Function  PrintManagement           Renames the specified printer.
Set-Printer                       Function  PrintManagement           Updates the configuration of an existing printer.
Set-PrinterProperty               Function  PrintManagement           Modifies the printer properties for the specified printer.
Write-PrinterNfcTag               Function  PrintManagement           Writes printer connection data to an NFC tag.
Out-Printer                       Cmdlet    Microsoft.PowerShell.U... Sends output to a printer.


So your script reduces to something like this:

$PrintersToRemove = @("Printer1", "Printer2")
$PrintDriversToRemove = @("Manufacturer1", "Manufacturer2")

Remove-Printer -Name $PrintersToRemove -WhatIf

$drivers = Get-PrinterDriver | ? Manufacturer -in $PrintDriversToRemove

foreach ($driver in $drivers)
{
    Remove-PrinterDriver -Name $driver.Name -WhatIf
}


Note that I didn't put in the removing excess printer driver logic. I'm not exactly sure what that does.

Note also the -WhatIf parameters. They stop the commands from actually executing. You would remove those when you are satisfied that the script will do what you want it to do.

Instead of putting comments at the top of the script, you can use the PowerShell help system:

&lt;#
.SYNOPSIS
  Remove old printers by name

.DESCRIPTION
  [Add stuff here if you like.]

.PARAMETER PrintersToRemove
  (REQUIRED) An array of printers to cycle through and
  remove. Use the exact name of the printer to have it included in the list

.PARAMETER PrintDriversToRemove - (optional) An array of printer drivers to cycle
  through and remove. You can obtain a list by running the "prndrvr.vbs -l"
  command on a computer with drivers to remove. Alternatively, you can
  leave the array empty, and the script will remove any unused (determined
  by the system) print drivers automatically.

.NOTES
  Written by Luke Barone

.EXAMPLE
  Example 1

.EXAMPLE 
  Example 2
#&gt;


That way you can do a help MyScript -detail

It's probably a good idea to use parameters to the script rather than getting people to edit it:

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [string[]]$PrintersToRemove,

    [Parameter(Mandatory=$false)]
    [string[]]$PrintDriversToRemove
    )


That way if you update the script, you can just copy it over the old one. The users don't have to merge them. You would make another script that contains the actual parameters, and that one would call this one.

One last thing, I like to put the following near the top of my scripts. It helps to avoid some bugs.

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'