Applies to Windows 8.1 or server R2
Windows 8.1 and Server R2 have a PowerShell component: "Printmanagement", which contains all the commands for managing both native and remote printers.
In the previous tip, we demonstrated how to read a printer task. Each print task has a property jobstatus that indicates whether the task was printed successfully.
All States can be obtained in this way:
Copy Code code as follows:
Ps> Import-module printmanagement
ps> [Microsoft.powershell.cmdletization.generatedtypes.printjob.jobstatus]::getnames ([ Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrintJob.JobStatus])
Normal
Paused
Error
deleting
Spooling
Printing
Offline
Paperout
Printed
Deleted
Blocked
Userintervention
Restarted
Complete
Retained
Renderinglocally
The next step is to filter the existing tasks. For example, do you want to list whether the print task has completed, or has encountered a failure:
Copy Code code as follows:
$ComputerName = $env: ComputerName
Get-printer-computername $ComputerName | Foreach-object {
Get-printjob-printername $_. Name-computername $ComputerName |
Where-object {$_. Jobstatus-eq ' Complete '-or $_. Jobstatus-eq ' Error '-or $_. Jobstatus-eq ' printed '}
}
Deleting a print task is also very simple, remove-printjob:
Copy Code code as follows:
$ComputerName = $env: ComputerName
Get-printer-computername $ComputerName | Foreach-object {
Get-printjob-printername $_. Name-computername $ComputerName |
Where-object {$_. Jobstatus-eq ' Complete '-or $_. Jobstatus-eq ' Error '-or $_. Jobstatus-eq ' printed '}
} |
Remove-printjob-cimsession $ComputerName