Skip to content

Instantly share code, notes, and snippets.

@0x9090
Created May 18, 2025 17:26
Show Gist options
  • Save 0x9090/7e7fb109311f94913e9919a9250d7d2a to your computer and use it in GitHub Desktop.
Save 0x9090/7e7fb109311f94913e9919a9250d7d2a to your computer and use it in GitHub Desktop.
Printer_Kicker.ps1
# Check if running as administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
# If not running as administrator, restart script with elevation
if (-not $isAdmin) {
Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
Exit
}
# Stop the Print Spooler service
Write-Host "Stopping Print Spooler service..."
Stop-Service -Name "Spooler" -Force
$spoolerStatus = Get-Service -Name "Spooler"
Write-Host "Print Spooler service status: $($spoolerStatus.Status)"
# Wait to ensure service is fully stopped
Start-Sleep -Seconds 5
# Delete all files in the printer spool directory (case insensitive)
$spoolPath = "C:\Windows\System32\spool\PRINTERS\"
Write-Host "Deleting files in $spoolPath..."
if (Test-Path -Path $spoolPath) {
$files = Get-ChildItem -Path $spoolPath -File -Force
if ($files.Count -gt 0) {
$files | Remove-Item -Force
Write-Host "Deleted $($files.Count) files from the spool directory."
} else {
Write-Host "No files found in the spool directory."
}
} else {
Write-Host "Spool directory not found."
}
# Start the Print Spooler service
Write-Host "Starting Print Spooler service..."
Start-Service -Name "Spooler"
$spoolerStatus = Get-Service -Name "Spooler"
Write-Host "Print Spooler service status: $($spoolerStatus.Status)"
Write-Host "Operation completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment