Home > Enterprise >  How do I get a true/false result from PowerShell if the results match a string?
How do I get a true/false result from PowerShell if the results match a string?

Time:10-01

I am trying to get a true/false or 0/1 response based on a series of commands. I might be approaching this wrong, but ultimately, I need to know if this exists.

PS > Get-VMNetworkAdapter -ManagementOS|Select -ExpandProperty SwitchName
LINK
Default Switch

I am looking to see if LINK exists then return true. So in the case above, a command that will return true since LINK exists.

CodePudding user response:

If the property SwitchName is a single item, you can use

(Get-VMNetworkAdapter -ManagementOS).SwitchName -eq 'LINK'

If that property contains an array of values as it appears to be, then I would go for

(Get-VMNetworkAdapter -ManagementOS).SwitchName -contains 'LINK'

The return value is a Boolean, either $true or $false

Both -eq and -contains work Case-Insensitive. If you need Case-Sensitive, use -ceq or -ccontains

  • Related