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 comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.