$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} }