-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_printer_queue.ps1
More file actions
53 lines (44 loc) · 1.62 KB
/
Copy pathclear_printer_queue.ps1
File metadata and controls
53 lines (44 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$ServiceName = 'Spooler'
$SpoolPath = 'C:\Windows\System32\spool\PRINTERS'
# Function to Check if the Script is Running as an Administrator
function Test-Administrator {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
if (-not (Test-Administrator)) {
Write-Error "This script needs to be run as an administrator."
exit 1
}
# Get the current status of the Spooler service
$service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($null -eq $service) {
Write-Warning "Spooler service not found!"
exit 1
}
Write-Host "Spooler service current status: $($service.Status)"
# Stop the service if it's running
if ($service.Status -eq 'Running') {
Write-Host "Stopping the Print Spooler service..."
Stop-Service -Name $ServiceName -Force
Start-Sleep -Seconds 2
}
else {
Write-Host "Print Spooler is not running; continuing..."
}
# Clear the spool folder if it exists
if (Test-Path $SpoolPath) {
Write-Host "Clearing print jobs from $SpoolPath..."
Get-ChildItem -Path $SpoolPath -File -ErrorAction SilentlyContinue | Remove-Item -Force
Write-Host "Print spooler queue cleared."
}
else {
Write-Warning "Spool folder not found: $SpoolPath"
}
# Start the service again
Write-Host "Starting the Print Spooler service..."
Start-Service -Name $ServiceName
Start-Sleep -Seconds 2
# Confirm final status
$finalStatus = (Get-Service -Name $ServiceName).Status
Write-Host "Spooler service is now: $finalStatus"