Disable Print Spooler Script

GCM313 1 Reputation point
2021-07-14T17:49:55.447+00:00

I created the script below to scan my list of computer and stop and disable print spooler service. It generates report, showing the service was stopped, but i check a computer on the list and it shows as running. Is something off with the script?

$computers = Get-Content -Path "C:\temp2\ComputerList.txt"
foreach ($computer in $computers) {
    if(!(Test-Connection -ComputerName $computer -Count 1 -Quiet))
    {
        Write-Output "$computer is offline"
        continue
    }
    $service = Get-Service -name Spooler -computername $computer
    $ServiceStatus = $service.Status
    $ServiceDisplayName = $service.DisplayName

    if ($ServiceStatus -eq 'Running') {
        write-output "$ServiceDisplayName is $ServiceStatus on $computer and needs to be disabled" | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
        Stop-Service -Name Spooler -Force -PassThru | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
        Set-Service -Name Spooler -StartupType Disabled
    }
    else {
        write-output "Status of $ServiceDisplayName is $ServiceStatus on $computer" | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
    }
}
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-07-14T18:44:26.16+00:00

    Do you run any type of monitoring service that might be restarting the service?

    Does the service have any "Recovery" options? You'll have to use the MMC to look for that, it's not exposed by either Get-Service or WMI.

    Is the services StartupType set to "disabled"? You might try setting the startuptype before you stop the service.

    0 comments No comments

  2. Brad M 1 Reputation point
    2021-07-15T12:32:49.017+00:00

    I believe the problem is line 14 and 15. You are stopping and setting the local spooler to disabled. You need to point back to the computer. Try this:

    Stop-Service -InputObject $(Get-Service -Computer $computer -Name spooler) -Force -PassThru | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
    Set-Service -InputObject $(Get-Service -Computer $computer -Name spooler) -StartupType Disabled