Shorten Where-Object

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Anthony Cohn Anthony Cohn 2 years, 3 months ago.

  • Author
    Posts
  • #18610
    Profile photo of Anthony Cohn
    Anthony Cohn
    Participant

    Hi,

    I am relatively new to PowerShell and couldn't figure out how to shorten this command in "splat-style" shortening so it would read better, is there a way?

    gwmi win32_service -ComputerName $servers -filter "StartMode = 'Auto' and State 'Running'" | where-object{($_.DisplayName -ne "Software Protection") -and ($_.DisplayName -ne "Shell Hardware Detection") -and ($_.DisplayName -ne "Microsoft .NET Framework NGEN v4.0.30319_x86") -and ($_.DisplayName -ne "Microsoft .NET Framework NGEN v4.0.30319_x64")} | ft SystemName, DisplayName

    I tried to assign everything inside the {} to a variable and then run it like that but that didn't work too well.

    Any suggestions would be appreciated.

    Thanks
    AC

  • #18614
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Well, you could build an array of strings and use the -notcontains operator:

    $exclude = "Software Protection", "Shell Hardware Detection", "Microsoft .NET Framework NGEN v4.0.30319_x86", "Microsoft .NET Framework NGEN v4.0.30319_x64"
    
    gwmi win32_service -ComputerName $servers -filter "StartMode = 'Auto' and State != 'Running'" |
    Where-Object { $exclude -notcontains $_.DisplayName }
    ft SystemName, DisplayName
    

    However, it's generally better to put all of this logic right into your -Filter string, for performance reasons. In that case, you can't use the array directly, but you can build up a filter string dynamically from the array. For example:

    $exclude = "Software Protection", "Shell Hardware Detection", "Microsoft .NET Framework NGEN v4.0.30319_x86", "Microsoft .NET Framework NGEN v4.0.30319_x64"
    
    $excludeAsString = @($exclude | ForEach-Object { "DisplayName != ""$_""" }) -join ' AND ' 
    
    gwmi win32_service -ComputerName $servers -filter "StartMode = 'Auto' and State != 'Running' AND $excludeAsString" |
    ft SystemName, DisplayName
    
  • #18621
    Profile photo of Anthony Cohn
    Anthony Cohn
    Participant

    Thank you Dave, that works perfectly!

You must be logged in to reply to this topic.