Powershell and -contains

Hi, 

I have a little problem with powershell and "contains"

In this situation works well and return "true"

$test = "where is the word"
($test).Contains("word")

but in this other return always "false" 

$test = Get-Process
($test).Contains("winlogon")

Why? 

January 11th, 2014 12:30am

Hi,

Try looking at the ProcessName property:

PS C:\Scripts\PowerShell Scripts\Misc Testing\1-10-2014> $test = Get-Process

PS C:\Scripts\PowerShell Scripts\Misc Testing\1-10-2014> $test.ProcessName.Contains('winlogon')
True

EDIT: If I remember correctly, I believe this requires PS3+ though.

EDIT2: This will work if you only have v2 (I'm sure there's a better way to do this, but this'll work in a pinch):

PS C:\> $found = $false
PS C:\> $test = Get-Process
PS C:\> $test | ForEach { If ($_.ProcessName.Contains('winlogon')) { $found = $true } }
PS C:\> $found
True

Free Windows Admin Tool Kit Click here and download it now
January 11th, 2014 12:46am

Hi Mike,

Thank you for your reply. 

Now I can find my missed "word" :-)

tnx

January 11th, 2014 9:55pm

Cheers, you're very welcome.
Free Windows Admin Tool Kit Click here and download it now
January 11th, 2014 10:15pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics