Powershell and -contains RRS feed

  • Question

  • Hi, 

    I have a little problem with powershell and "contains"

    In this situation works well and return "true"

    $test = "where is the word"

    but in this other return always "false" 

    $test = Get-Process



    Andrea Gallazzi - blog:
    This posting is provided AS IS with no warranties, and confers no rights.

    Friday, January 10, 2014 9:30 PM


  • 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')

    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

    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    • Edited by Mike Laughlin Friday, January 10, 2014 10:07 PM
    • Marked as answer by Andreagx Saturday, January 11, 2014 6:43 PM
    Friday, January 10, 2014 9:46 PM

All replies