stringpowershellwildcardmember-enumeration

How to match any part of a property string using Where-Object


I have an array $array with many elements, an example looks like this:

ProfileID         : 100
UID               : 17
Name              : SharePoint
Description       : SharePoint Server Description

Now I am trying to filter by Name property, string to match is:

$string
SharePoint Policy Assignment

I have tried:

$array | Where-Object {$_.Name -like "$string"}
no match

$array | Where-Object {$_.Name -like "$string*"}
no match

$array | Where-Object {$_.Name -match "$string"}
no match

Is this possible using PowerShell? What am I missing?


Solution

  • The -like operator in PowerShell is used for a wildcard match, so you need to use the wildcard character, the asterisk, *.

    Imagine this scenario, where I'm trying to match a particular Windows Service.

    $svcs = Get-Service | Select-Object -first 15
    
    C:\temp\blog> $svcs
    
    Status   Name               DisplayName                           
    ------   ----               -----------                           
    Stopped  AJRouter           AllJoyn Router Service                
    Stopped  ALG                Application Layer Gateway Service     
    Running  AMD External Ev... AMD External Events Utility           
    Stopped  AppIDSvc           Application Identity                  
    Running  Appinfo            Application Information               
    Stopped  AppMgmt            Application Management                
    Stopped  AppReadiness       App Readiness                         
    Stopped  AppVClient         Microsoft App-V Client                
    Stopped  AppXSvc            AppX Deployment Service (AppXSVC)     
    Stopped  aspnet_state       ASP.NET State Service                 
    Stopped  AssignedAccessM... AssignedAccessManager Service         
    Running  AsSysCtrlService   ASUS System Control Service           
    Running  AudioEndpointBu... Windows Audio Endpoint Builder        
    Running  Audiosrv           Windows Audio                         
    Running  AUEPLauncher       AMD User Experience Program Launcher  
    

    To use the -Like operator to get a match, I have to provide a Wildcard Character, like this.

    $svcs | Where-Object Name -like App*
    
    Status   Name               DisplayName                           
    ------   ----               -----------                           
    Stopped  AppIDSvc           Application Identity                  
    Running  Appinfo            Application Information               
    Stopped  AppMgmt            Application Management                
    Stopped  AppReadiness       App Readiness                         
    Stopped  AppVClient         Microsoft App-V Client                
    Stopped  AppXSvc            AppX Deployment Service (AppXSVC)     
    

    Try your operation using a WildCard, and I bet it will work :)

    One other thing I noted, your $string is equal to SharePoint Policy Assignment, but the column you're comparing on of .Name is just SharePoint.