Friday, 26 August 2016

Powershell - Contains Check In Where Object

You can use the Where-Object cmdlet to filter objects from a collection based on their property values. We can use the comparison operator like with wildcard character to check if a property of object contains a specific string.
Note: You can not use the comparison operator contains to check the contains string, because it's designed to tell you if a
collection of objects includes ('contains') a particular object
The following command returns all the processes in current machine.
Get-Process | Select ID,ProcessName
If you want to list only the process which contains string '32' in processname, you need to use the below command.
Get-Process | Where-Object { $_.Name -like '*32*' }
By default like operator ignore the case-sensitive check. To perform a case-sensitive comparison just prefix the word "c" with like operator ("clike").
Get-Process | Where-Object { $_.Name -clike '*rundll*' }
If you want to check starts with string, you can use the same like operator with appending * at the end of the string alone.
Get-Process | Where-Object { $_.Name -like 'rundll*' }
Likewise, to check ends with string, need to append * at start of the string.
Get-Process | Where-Object { $_.Name -like '*host' }


No comments:

Post a Comment