Skip to content

Set-Printer

Print Management: Updates printer settings

#Requires -Version 5.1
#Requires -Modules PrintManagement

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)][string]$PrinterName,
    [string]$DriverName,
    [string]$PortName,
    [string]$ComputerName,
    [pscredential]$AccessAccount
)

Process {
    try {
        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 }
        $cmdArgs = @{ ErrorAction = 'Stop'; CimSession = $cim; ComputerName = $ComputerName; Name = $PrinterName; Confirm = $false }
        if (-not [System.String]::IsNullOrWhiteSpace($DriverName)) { $cmdArgs.Add('DriverName', $DriverName) }
        if (-not [System.String]::IsNullOrWhiteSpace($PortName)) { $cmdArgs.Add('PortName', $PortName) }
        $result = Set-Printer @cmdArgs | Select-Object *
        if ($null -ne $result) { $result | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force }
    }
    catch { throw }
    finally { if ($null -ne $cim) { Remove-CimSession $cim -ErrorAction SilentlyContinue } }
}

Printer to update

New driver name

New port name

Computer hosting the printer

Optional credential

An interactive directory of PowerShell scripts.