Skip to content

Instantly share code, notes, and snippets.

@SkyyySi
Last active March 27, 2025 11:07
Show Gist options
  • Save SkyyySi/8ae41f3b9e40812669e515cfd6ac357e to your computer and use it in GitHub Desktop.
Save SkyyySi/8ae41f3b9e40812669e515cfd6ac357e to your computer and use it in GitHub Desktop.
A small PowerShell script to check if one or more shared printer(s) is known on a given printer server
# SPDX-License-Identifier: 0BSD
# Author: Simon Buschendorf (github.com/SkyyySi)
#
# To invoke this script, run it like this:
#
# powershell.exe -File '.\Test-Printer.ps1' -Name 'foo'
#
# Or, for multiple printers, like this:
#
# powershell.exe -Command "& '.\Test-Printer.ps1' -Name @('foo', 'bar')"
param (
[Parameter(Mandatory=$true)]
[string[]]$Name,
[string]$ComputerName = $null,
[ValidateSet('Hashtable', 'Array', 'Boolean')]
[string]$OutputType = 'Hashtable'
)
$ErrorActionPreference = 'Stop'
#Write-Host "`$Name = @($($Name -join ', ')) [$($Name.Length)]"
#Write-Host "`$ComputerName = ${ComputerName}"
#Write-Host "`$OutputType = ${OutputType}"
[ciminstance[]]$knownPrinters = if ($ComputerName) {
Get-Printer -ComputerName $ComputerName
} else {
Get-Printer
}
[string[]]$knownPrinterNames = ($knownPrinters | ForEach-Object { $_.Name })
[System.Collections.Generic.Dictionary[string, bool]]$printerAvailabilityTable = @{}
$Name | ForEach-Object {
$printerAvailabilityTable[$_] = ($_ -in $knownPrinterNames)
}
switch ($OutputType) {
'Hashtable' {
$printerAvailabilityTable
}
'Array' {
$printerAvailabilityTable.GetEnumerator() | Where-Object {
$_.Value
} | ForEach-Object {
$_.Value
}
}
'Boolean' {
$false -notin $printerAvailabilityTable.Values
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment