Watch Demo×
×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

How to Use PowerShell to Clear Print Queues

We all rely on printers, whether in office setups or when working remotely. But, as essential as they are, printers can sometimes be finicky. For IT professionals and Managed Service Providers (MSPs), maintaining the efficiency of print servers is paramount. So when printer issues arise, having an effective method to solve them becomes invaluable.

Understanding Print Queues

A print queue is a temporary storage area for print jobs that are waiting to be printed. When you send a document to a printer, it doesn’t go directly to the printer hardware. Instead, it first finds its way to the print queue. From there, the print queue efficiently manages these jobs, sending them to the printer when ready.

However, like any system, the print queue can face issues. Maybe a particular job gets stuck, preventing subsequent jobs from processing. Perhaps a printer goes offline, causing the queue to pile up. These disruptions can cause delays and operational inefficiencies.

This is where the PowerShell script discussed in this blog post becomes invaluable. It’s designed to clear all jobs from the print queue, ensuring smooth printing operations. To leverage this script, make sure you have administrator privileges.

The Script

#Requires -Version 5.1

<#
.SYNOPSIS
    Clears Print Queue for all printers
.DESCRIPTION
    Clears Print Queue for all printers.
    This script will stop the printer spooler service, clear all print jobs, and start the printer spooler service.
    If some print jobs are not cleared, then a reboot might be needed before running this script again.
.EXAMPLE
    No parameters needed
.OUTPUTS
    String
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes:
    Initial Release
    (c) 2023 NinjaOne
    By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
.COMPONENT
    Printer
#>

[CmdletBinding()]
param ()

begin {
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
}

process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }
    Write-Host "Stopping print spooler service"
    $StopProcess = Start-Process -FilePath "C:WINDOWSsystem32net.exe" -ArgumentList "stop", "spooler" -Wait -NoNewWindow -PassThru
    # Exit Code 2 usually means the service is already stopped
    if ($StopProcess.ExitCode -eq 0 -or $StopProcess.ExitCode -eq 2) {
        Write-Host "Stopped print spooler service"
        # Sleep just in case the spooler service is taking some time to stop
        Start-Sleep -Seconds 10
        Write-Host "Clearing all print queues"
        Remove-Item -Path "$env:SystemRootSystem32spoolPRINTERS*" -Force -ErrorAction SilentlyContinue
        Write-Host "Cleared all print queues"

        Write-Host "Starting print spooler service"
        $StartProcess = Start-Process -FilePath "C:WINDOWSsystem32net.exe" -ArgumentList "start", "spooler" -Wait -NoNewWindow -PassThru
        if ($StartProcess.ExitCode -eq 0) {
            Write-Host "Started print spooler service"
        }
        else {
            Write-Host "Could not start Print Spooler service. net start spooler returned exit code of $($StartProcess.ExitCode)"
            exit 1
        }
    }
    else {
        Write-Host "Could not stop Print Spooler service. net stop spooler returned exit code of $($StopProcess.ExitCode)"
        exit 1
    }
    exit 0
}

end {}

 

Access over 300+ scripts in the NinjaOne Dojo

Get Access

Security Implications of the Script

While this PowerShell script is a boon for managing print queues, it’s essential to be aware of its security implications:

  1. Administrator Privileges: The script demands admin rights since it interacts with system-level services, such as the print spooler. If you aren’t an administrator, you shouldn’t be running this script.
  2. Potential Vulnerabilities: If not properly secured, malicious actors could exploit the script, deleting print jobs or even disrupting services. Always ensure the script is sourced from trusted sources and is kept in secure locations.
  3. Consultation: If you have any reservations regarding the script’s security, it’s always wise to consult with an IT security professional.

Other Quick Tips for Troubleshooting Printer Problems

Encountering printer issues? Beyond using the PowerShell clear print queue script, here are a few general troubleshooting steps:

  1. Power Check: Ensure the printer’s power cord is plugged in and the device is turned on.
  2. Connectivity: Double-check the printer’s connection to your computer.
  3. Test Page: Print a test page to confirm if the printer functions properly.
  4. Restart: Often, a simple restart of both your computer and the printer can resolve many issues.
  5. Manufacturer Support: If issues persist, consider contacting the printer’s manufacturer for dedicated support.

Final Thoughts

In the dynamic landscape of IT, tools that streamline operations and solve real-world problems are invaluable. This PowerShell clear print queue script is one such tool, ensuring efficient and smooth printing operations. Always approach such scripts with an understanding of their purpose, benefits, and potential implications.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).