Description

This PowerShell script helps you create an SCCM package that clears the print spooler on target systems. Clearing the print spooler can resolve issues with stuck print jobs, which is a common problem in enterprise environments. Deploying this script via SCCM ensures that the fix is applied consistently across all affected systems.

PowerShell Script

# Define Variables
$ScriptName = "Clear-Print-Spooler"
$ScriptDescription = "Clears the print spooler to resolve stuck print jobs and improve printer reliability."
$PackageSourcePath = "C:\Scripts\$ScriptName"

# Load SCCM PowerShell Module
Import-Module "$($Env:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" -Force
$SiteCode = "YourSiteCode"
cd "$SiteCode`:"

# Check if a package with the same name exists and delete it
$ExistingPackages = Get-CMPackage -Name $ScriptName -ErrorAction SilentlyContinue
if ($ExistingPackages) {
    foreach ($Package in $ExistingPackages) {
        Remove-CMPackage -Id $Package.PackageID -Force
    }
}

# Create the Package Source Directory if it does not exist
if (!(Test-Path -Path $PackageSourcePath)) {
    New-Item -ItemType Directory -Path $PackageSourcePath -Force
}

# Create the PowerShell script
$ScriptContent = @'
Stop-Service -Name Spooler -Force
Remove-Item -Recurse -Force -Path "C:\Windows\System32\spool\PRINTERS\*"
Start-Service -Name Spooler
'@

$ScriptPath = "$PackageSourcePath\$ScriptName.ps1"
Set-Content -Path $ScriptPath -Value $ScriptContent

# Create the SCCM Package
$Package = New-CMPackage `
    -Name $ScriptName `
    -Description $ScriptDescription `
    -Path $PackageSourcePath

# Create a Program for the Package
$Program = New-CMProgram `
    -PackageName $ScriptName `
    -StandardProgramName $ScriptName `
    -CommandLine "powershell.exe -ExecutionPolicy Bypass -File $ScriptPath" `
    -ProgramRunType "WhetherOrNotUserIsLoggedOn"

# Distribute the Package to the Distribution Point but do not deploy
Start-CMContentDistribution `
    -PackageName $ScriptName `
    -DistributionPointName "YourDistributionPointName"

Explanation

This script creates an SCCM package that clears the print spooler on all targeted systems. This is particularly useful for resolving issues with stuck print jobs that can cause delays and frustration for users. By deploying this fix via SCCM, you ensure that it is applied consistently across your environment.

Tags:

#PrinterIssues #PowerShell #PrintSpooler #SCCM