Skip to content

Instantly share code, notes, and snippets.

@Xeckt
Last active November 21, 2022 13:39
Show Gist options
  • Save Xeckt/1ff54f46da7d48863ea9f028583afca6 to your computer and use it in GitHub Desktop.
Save Xeckt/1ff54f46da7d48863ea9f028583afca6 to your computer and use it in GitHub Desktop.
Clear that pesky printer spool when the queue is just too large!
#Requires -Version 7.3
$global:UserInput = $null
$global:PrintPipelineProcess = "printfilterpipelinesvc"
$global:PrintPipelineExe = "C:\Windows\System32\printfilterpipelinesvc.exe"
$global:SpoolerService = "spooler"
$global:SpoolDirectory = "C:\Windows\System32\spool\PRINTERS"
$global:CurrentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
function GetItemsInQueue() {
Write-Host "Checking printer queue information..."
$GetPrintObjects = Get-CimInstance -ClassName Win32_PerfFormattedData_Spooler_PrintQueue | Select-Object Name, @{Expression = {$_.jobs} ; Label = "CurrentJobs"}, TotalJobsPrinted, JobErrors | Out-String
Write-Host $GetPrintObjects
$global:UserInput = Read-Host "Would you like to go ahead and proceed clearing the printer spool? [y/n]"
}
function ClearSpool() {
if (Get-Process -Name "$global:PrintPipelineProcess" -ErrorAction SilentlyContinue) {
Write-Host "[INFO] -> Pipeline service exe is running, stopping"
Stop-Process -Name $global:PrintPipelineProcess
if ($?) {
Write-Host "[SUCCESS] -> Pipeline service exe has stopped"
} else {
Write-Host "[ERROR] -> Unable to stop pipline process"
}
} else {
Write-Host "[INFO] -> Pipeline service process exe is not running"
}
Write-Host "[INFO] -> Checking for spool files in: $global:SpoolDirectory"
$FileCount = Get-ChildItem -Path $global:SpoolDirectory -recurse | Measure-Object | Select-Object -ExpandProperty Count
Remove-Item -Path $global:SpoolDirectory/* -Recurse
if ($?) {
Write-Host "[INFO] -> Removal of spool files successful."
Write-Host "[INFO] -> Starting printer pipeline service"
if (Start-Process -FilePath "$global:PrintPipelineExe") {
if ($?) {
Write-Host "[INFO] -> Starting printer pipeline successful"
} else {
Write-Host "[ERROR] -> Unable to start printer pipeline service, start it manually at: $global:PrintPipelineExe"
}
}
return $true
} else {
return $false
}
}
function SpoolService($Status) {
$StatusObject = Get-Service -Name $global:SpoolerService | Select-Object -ExpandProperty Status
if ($Status -eq $null) {
Write-Error "[ERROR] -> No parameter supplied to SpoolService function, exiting."
Exit-PSSession
}
if ($Status -ieq "start") {
if ($StatusObject -ieq "running") {
Write-Host "[INFO] -> Service is already running! Nothing to do."
} else {
Start-Service $global:SpoolerService
if ($?) {
Write-Host "[INFO] -> Service $global:SpoolerService started successfully."
} else {
Write-Error "[ERROR] -> Couldn't start service $global:SpoolerService, exiting"
Exit-PSSession
}
}
}
if ($Status -ieq "stop") {
if ($StatusObject -ieq "running") {
Write-Host "[INFO] -> Service $global:SpoolerService is running, stopping now"
Stop-Service $global:SpoolerService
if ($?) {
Write-Host "[INFO] -> Service stopped successfully."
} else {
Write-Error "[ERROR] -> Couldn't stop the spooler service, exiting."
Exit-PSSession
}
} else {
Write-Host "[INFO] -> Service $global:SpoolerService is already Stopped! Nothing to do."
}
}
}
function Main() {
GetItemsInQueue
if (!$?) {
Write-Error "[ERROR] - Something went wrong"
Read-Host "Press any key to exit."
Exit-PSHostProcess
}
if ( $global:UserInput -ieq "y") {
Write-Host "`n[INFO] -> Stopping the Print Spooler service."
SpoolService("Stop")
if (ClearSpool -eq "nospool" -or ClearSpool) {
Write-Host "[INFO] -> Restarting print spool service"
SpoolService("Start")
Get-Service $global:SpoolerService
}
} else {
Write-Error "[INFO] -> Exiting due to user denying continuation of script."
Exit-PSSession
}
}
if (!$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Error: Insufficient privileges to run script" -ForegroundColor White -BackgroundColor Red
Read-Host "Any key to exit"
exit
} else {
Main
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment