Skip to content

Instantly share code, notes, and snippets.

@iyre
Last active February 20, 2020 16:37
Show Gist options
  • Save iyre/2c0d51bd5ec77c55e323a7b2c4b02f7f to your computer and use it in GitHub Desktop.
Save iyre/2c0d51bd5ec77c55e323a7b2c4b02f7f to your computer and use it in GitHub Desktop.
Remotely clear print queue and restart spooler
$ComputerName = Read-Host -Prompt 'Target'
$Cred = Get-Credential -Credential $null
Write-Host "Opening session with remote host"
$Session = New-PSSession -ComputerName $ComputerName -Credential $Cred -ErrorAction Stop
$Confirm = Read-Host "Are you sure that you want to delete all print jobs on $ComputerName (Y/N)"
if ( $Confirm -eq 'Y' ) {
Write-Host "1. Stopping spooler" -ForegroundColor Green
Invoke-Command -Session $Session -ScriptBlock { Stop-Service spooler -Force }
Write-Host "2. Removing print jobs" -ForegroundColor Green
Invoke-Command -Session $Session -ScriptBlock { Remove-Item -Path $env:windir\system32\spool\PRINTERS\*.* }
Write-Host "3. Starting spooler" -ForegroundColor Green
Invoke-Command -Session $Session -ScriptBlock { Start-Service spooler }
}
Write-Host "Removing remote session"
Remove-PSSession $Session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment