Skip to content

Get-PrintJob

Print Management: Retrieves the print job on the specified printer.

#Requires -Version 5.1
#Requires -Modules PrintManagement

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$PrinterName,
    [Parameter(Mandatory = $true)]
    [int]$JobID,
    [string]$ComputerName,
    [pscredential]$AccessAccount,
    [ValidateSet('*', 'ID', 'JobStatus', 'DocumentName', 'UserName', 'Position', 'Size', 'PagesPrinted', 'TotalPages', 'SubmittedTime', 'Priority')]
    [string[]]$Properties = @('ID', 'JobStatus', 'DocumentName', 'UserName', 'Position', 'Size', 'PagesPrinted', 'TotalPages', 'SubmittedTime', 'Priority')
)

Process {
    try {
        if ($Properties -contains '*') { $Properties = @('*') }
        elseif ($null -eq ($Properties | Where-Object { $_ -like 'ID' })) { $Properties += 'ID' }
        if ([System.String]::IsNullOrWhiteSpace($ComputerName)) { $ComputerName = [System.Net.DNS]::GetHostByName('').HostName }
        $cim = if ($null -eq $AccessAccount) { New-CimSession -ComputerName $ComputerName -ErrorAction Stop }
               else { New-CimSession -ComputerName $ComputerName -Credential $AccessAccount -ErrorAction Stop }
        $job = Get-PrintJob -CimSession $cim -PrinterName $PrinterName -ComputerName $ComputerName -ID $JobID -ErrorAction Stop |
            Select-Object $Properties | Sort-Object ID
        $job | ForEach-Object { $_ | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru }
        Write-Output $job
    }
    catch { throw }
    finally { if ($null -ne $cim) { Remove-CimSession $cim -ErrorAction SilentlyContinue } }
}

Name of the printer from which to retrieve the print job.

ID of the print job to retrieve.

Name of the computer from which to retrieve the print job.

User account that has permission to perform this action.

List of properties to expand; use * for all properties.

An interactive directory of PowerShell scripts.