How to find out if a printer or printer port is installed?

    General discussion

  • I am developing a script that installs certain printers based on input from a CSV file.  The file works great as long as it is the first time being run.  I am having some trouble in some cases with running the script on computers that already have one or more printers or printer ports installed that I am trying to install with this script.

    So I am trying to generate an if test to see if various printers and printer ports are present.

    Here is a little test script I have created that is trying to see if a given printer port is already installed:


    $PortName = ""
    $Ports = Get-WmiObject -Class Win32_TCPIPPrinterPort | Select-Object Name
    If ($Ports -contains $PortName)
    	Write-Host "Found port" $PortName
    	Write-Host "Didn't find" $PortName


    If I run "Get-WmiObject -Class Win32_TCPIPPrinterPort | Select-Object Name" on my computer, I get this as output:


    However, when I run my first script on my computer, I receive the message "Didn't find".

    I presume the problem is that $Ports is a variable that contains an object and $PortName is a variable that contains a string.  So I am guessing that I need to turn $Ports into a collection of strings...  is that right?  And if so, how do I do that?  I feel like there is an easy way to accomplish this that I am just missing.

    Thursday, October 20, 2011 5:33 PM

All replies