We help IT Professionals succeed at work.

Revising Powershell Script that check Print Queue

compdigit44 asked
A number of months ago ,  EE expert " Joshua Grantom "wrote the script below that checked a printer UNC path and sent the results to CSV listing if it was up or down. I have noticed when the script hist a queue it cannot connect to it stops the script from continuing any suggestions on how to correct this

# Script Author: Joshua Grantom 
# Version: 1.1
# Purpose: Checks a list of Printers by UNC path to see if they are connectable

#Loads Windows Forms Assembly
#Printer List (1 UNC Printer path per line)
$printers = Get-Content "c:\PrintQueues.txt"
#CSV File to Output to
$outputfile = "c:\PrintQueues.csv"
#Adds Header to CSV File
"Printer UNC Path `t Connection Status" | Out-File $outputfile -append
#Tries to connect to each printers queue (does not require drivers to be installed)
foreach ($printer in $printers) {
Invoke-Expression -command "rundll32 printui,PrintUIEntry /o /n '$printer'"
#Sleep time in seconds until script resumes (can increase if it takes longer to query printer)
Start-Sleep -s 2
$window = Get-Process rundll32 | Where-Object {$_.MainWindowTitle -like "*"} | Select MainWindowTitle | ft -HideTableHeaders | Out-String
    #If rundll32 window title is blank, usually means error, write error
    "$printer`t Error Connecting" | Out-File $outputfile -append
    #If rundll32 window title is not blank, usually means queue is opened, write success
    "$printer`t Successfully Connected" | Out-File $outputfile -append
# Closes active window (print queue or error message)

Open in new window

Watch Question

#Sleep time in seconds until script resumes (can increase if it takes longer to query printer)
Start-Sleep -s 2

Have you tried increasing this to 5 or 10 to see if the script behaves correctly?


I increased the Sleep time to 10 which did help but when the scritp hit a queue that is error out on... Access Denied etc... Is seems to get hung up and stops
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Seems as sending Ctrl-F4 to close the (error) window does not work in that case. The script sends the key, and waits until it has been received, which might not happen. Is there another window on top not allowing to get closed with Ctrl-F4?


Let me try this again tomorrow and will let you know... thanks again..


When a error dialog box pops up it does not always stop the script from continue on. There does not appear to be any other dialog box open either. Anyway to have the script continue on when an error is encountered.

Thank you for all of our hlpe