PowerShell

Windows Server/Windows 10: How to delete stuck Print Jobs with PowerShell (Spooler)

This week I had the task to delete print jobs from the queue. However, some of them could not be removed. So I decided to write an article about it and also a PowerShell script. Deleting print jobs on a print server involves 3 tasks: Stop the spooler service. Delete all content in den spool directory. And finally start the spooler service. Let’s put them all together in a PowerShell script.

The Functionality

The script does the following:

  • stops the spooler service
  • deletes all content of C:\Windows\System32\spool\PRINTERS\
  • starts the spooler service

This guarantees that all stuck print jobs are deleted. Hopefully 😉

In Action …

Are you sure you wanna do this? Consider, you will delete all print jobs!

1.PNG

Press Y.

Unbenannt.PNG

There is also an error state. For example, when you cannot start or stop the spooler service. Further errors are output via the error handling of PowerShell. Checking the folder doesn’t make sense. In the meantime, more print jobs could be coming in.

Unbenannt.PNG

The Script

Last, but not least the script itself. It was a quick job and it’s not perfect but it does what it’s supposed to do. End of Subject.

do {
Write-Host '\\\\\\\\\\\\\\ This script deletes all print jobs. \\\\\\\\\\\\\\'
''
$sure=Read-Host "Are you sure you want to delete all Print Jobs on $env:computername (Y/N)"
''
If ($sure -eq 'Y')

{
'================================================================='

Stop-Service spooler
Write-Host '1. Stopping Spooler Service ...' -ForegroundColor Green
Remove-Item -Path $env:windir\system32\spool\PRINTERS\*.*
Write-Host "2. Clearing content in $env:windir\system32\spool\PRINTERS" -ForegroundColor Green
Write-Host '3. Starting Spooler Service ...' -ForegroundColor Green
$start=Start-Service Spooler -ErrorAction Ignore
If ((Get-Service spooler).status -eq 'Stopped')
{Write-Host '!!! Error. Spooler could not be started or stopped. Check Service. !!!' -ForegroundColor Red}

'================================================================='
''

PAUSE
}
}
while ($sure -ne 'N')

Copy the code in PowerShell ISE and save it as a ps1 file. By default Windows opens ps1 files with notepad. That’s not good. You can change the settings by modifying the default program settings for file extensions. But the best way (also regarding to the Windows UAC) is to store the file somewhere and put a link on your desktop.

Paste it on your desktop via a link

Well, there are many ways to call up this script. For example, you can put it into a function or you can run it via a link. My script is located on C:\PowerShell and the name of the file is Clear_Print_Jobs.ps1.

1.PNG

Next create a shortcut on your desktop.

2.PNG

Now put the following line into the location path:


powershell -noprofile -command "&{start-process powershell -ArgumentList '-noprofile -file "C:\PowerShell\Clear_Print_Jobs.ps1"' -verb RunAs}"

3.PNG

Click next and specify a name for the shortcut.

Unbenannt.PNG

Click Finish.

Unbenannt.PNG

Right click and select “Run as administrator” and have fun deleting stuck print jobs!

See you next time with PowerShell!

5 replies »

  1. Hi Walden!

    Thank you for the kind words. Just take line 11,13 and 16 and save them in a new ps1. file.
    Then put the new file into a scheduled task.

    Let me know if it works!
    Best,
    P

    Like

  2. This is awesome, and super helpful for a couple problem printers I have on our print server that seem to always want to have jobs hang.
    I do have a question though, I’m fairly new to PowerShell, so I’m not sure if this is something super obvious that I just don’t know or not, but is there any way to set this up where it can run without the checks? For example, if I wanted to run it via a scheduled task every so often so I don’t have to manually go in and clear the queue.

    Thanks!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.