Powershell | Cleanup all the printer spooler queues

$computers = @("COMP01","COMP02")
foreach ($computer in $computers) {
echo $computer

if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $computer -Quiet) {
    echo "ON"
    #Get-WmiObject -computer $computers Win32_Service -Filter "Name='Spooler'"| ft systemname, name, state

    #set your first argument as $computer 
    #$computer = $args[0] 
    #Stop the service: 
    Get-WmiObject -Class Win32_Service -Filter 'name="spooler"' -ComputerName $computer | Invoke-WmiMethod -Name StopService | out-null 
    #Delete all items in: C:\windows\system32\spool\printers on the remote computer 
    #the `$ is the escape charater to make sure powershell sees the $ as a charater rather than a variable 
    Get-ChildItem "\\$computer\C`$\windows\system32\spool\printers" | Remove-Item 
    #Start the service 
    Set-Service spooler -ComputerName $computer -status Running
    }
    else {Out-Null}
}

 


 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

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

Twitter picture

You are commenting using your Twitter 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.