none
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 = "10.1.2.68_1"
    $Ports = Get-WmiObject -Class Win32_TCPIPPrinterPort | Select-Object Name
    
    If ($Ports -contains $PortName)
    {
    	Write-Host "Found port" $PortName
    }
    Else
    {
    	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:

    Name
    ----
    10.1.2.68
    10.1.2.68_1


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

    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