Fatal Ignorance
Fatal Ignorance

Reputation: 3

Powershell Comparison Operators not functioning when trying to compare vs command output

I am new to powershell scripting so, sorry if I'm doing something obviously wrong.

Here is the bit of the script that is not working:

$PrinterList= Get-WMIObject -ClassName Win32_Printer |Select-Object Name
$SelectedPrinter = "Microsoft Print to PDF"

if( $Printerlist -NotContains $SelectedPrinter ){[void][System.Windows.MessageBox]::Show("Failure!")}
Else{[void][System.Windows.MessageBox]::Show("success")}

Here is the output of $PrinterList on my computer:

OneNote
Microsoft XPS Document Writer
Microsoft Print to PDF
Fax

As you can see $SelectedPrinter should be contained in $PrinterList but when I run the script it returns as if it does not. What am I doing wrong?

Upvotes: 0

Views: 90

Answers (1)

PSgold
PSgold

Reputation: 251

Try if($Printerlist.Name -NotContains $SelectedPrinter){}

Or leave if($Printerlist -NotContains $SelectedPrinter){}

and change to $PrinterList = (Get-WMIObject -ClassName Win32_Printer |Select-Object Name).Name

Upvotes: 1

Related Questions