0
\$\begingroup\$

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 {
 & .\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 {
 & .\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
}
Dan Oberlam
8,1392 gold badges33 silver badges74 bronze badges
asked Apr 4, 2018 at 17:15
\$\endgroup\$
5
  • 1
    \$\begingroup\$ syntax highlighting looks off, and at a glance not sure if it's because the code is broken or because the syntax highlighter SE uses is bad \$\endgroup\$ Commented Apr 4, 2018 at 18:35
  • \$\begingroup\$ @CanadianLuke in order to function!" misses an introducing " character. \$\endgroup\$ Commented Apr 4, 2018 at 19:43
  • \$\begingroup\$ @Dannnno PowerShell is not a supported language for the syntax highlighter. \$\endgroup\$ Commented Apr 4, 2018 at 19:45
  • \$\begingroup\$ @200_success Well I've checke that, and if the (apparently) missing " character is put in, it renders fine. \$\endgroup\$ Commented Apr 4, 2018 at 19:46
  • 2
    \$\begingroup\$ @CanadianLuke iirc PowerShell needs @" "@ for a multi-line string \$\endgroup\$ Commented Apr 4, 2018 at 20:01

1 Answer 1

3
\$\begingroup\$

Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available:

PS> 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:

<#
.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
#>

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'
answered Apr 5, 2018 at 2:14
\$\endgroup\$
3
  • \$\begingroup\$ Just curious about the lines Set-StrictMode -Version Latest as you said it helps your avoid bugs. Can you say a little bit more about how that helps avoid bug and what that line means? \$\endgroup\$ Commented Aug 1, 2018 at 17:52
  • 1
    \$\begingroup\$ @joshgoldeneagle, if you type help Set-StrictMode -detail, and look under the Version switch, you will see exactly what it does. (Look under "1.0" and "2.0" since "Latest" includes those.) By default, if you refer to some variable that isn't defined, then PS doesn't care. It doesn't consider it an error. But what if you just made a typing mistake, typing $fooo instead of $foo for instance? Strict mode would turn that into an error. It does stuff like that. \$\endgroup\$ Commented Aug 3, 2018 at 8:31
  • \$\begingroup\$ Excellent, sounds like StrictMode will keep me in line! Or at least my Powershell code. Thank you. \$\endgroup\$ Commented Aug 3, 2018 at 19:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.