Last active
February 20, 2020 16:37
-
-
Save iyre/2c0d51bd5ec77c55e323a7b2c4b02f7f to your computer and use it in GitHub Desktop.
Remotely clear print queue and restart spooler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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