Skip to content

Initialize-PrintTestpage

Print Management: Prints a test page

#Requires -Version 5.1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$PrinterName,
    [string]$ComputerName
)

Process {
    try {
        if ([System.String]::IsNullOrWhiteSpace($ComputerName)) { $ComputerName = [System.Net.DNS]::GetHostByName('').HostName }
        if ($ComputerName -eq [System.Net.DNS]::GetHostByName('').HostName) {
            $printer = Get-WmiObject -Class Win32_Printer -Filter "Name='$PrinterName'" -ErrorAction Stop
        }
        else {
            $printer = Get-WmiObject -Class Win32_Printer -ComputerName $ComputerName -Filter "Name='$PrinterName'" -ErrorAction Stop
        }
        if ($null -eq $printer) { throw "Printer '$PrinterName' not found" }
        $result = $printer.PrintTestPage()
        [PSCustomObject]@{ Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; Status = "Success"; PrinterName = $PrinterName; ComputerName = $ComputerName; ReturnValue = $result.ReturnValue }
    }
    catch { throw }
}

Name of the printer to test

Computer hosting the printer

An interactive directory of PowerShell scripts.