The -like operator allows us to use wildcards to select required objects.
For example, like this.
Get-Service | Where-Object Description -like "*wsman*"
Or else
Get-Service | Where-Object Description -like "*w?man*"
By default, this operator is not case-sensitive, so the following commands are equivalent.
Get-Service | Where-Object Description -like "*wsman*" Get-Service | Where-Object Description -like "*WSman*" Get-Service | Where-Object Description -like "*WSMAN*"
If we want it to consider the case of the objects it examines, we can specify it as -clike.
Get-Service | Where-Object Description -clike "*wsman*"
On the other hand, if we want to be clear, that the operator is case-insensitive, we can spell it as -ilike.
Get-Service | Where-Object Description -ilike "*wsman*"
Besides, there are opposite operator in PowerShell, -notlike.
Get-Service | Where-Object StartupType -notlike "Auto*"
Which, as the -like operator, has two variations: -cnotlike and -inotlike.
Result
Like many other comparison operators, -like returns different results, depending on if the object it acts upon is scalar or an array.
If argument is scalar, then the result is a bool object, i.e. True or False.
"Some string." -like "*string*"
True
"Some string." -like "*integer*"
False
But if we pass it an array, then the result will consist of the elements, that meet the condition.
"Winter", "Spring", "Summer", "Autumn" -like "*er"
Winter Summer
Combine
Besides simple comparisons, -like and -notlike operators can be used to specify more complex expressions.
Let’s start with getting all the services, that depend on the Remote Procedure Call (RPC) service.
Get-Service | Where-Object { $_.RequiredServices.Name -like "RpcSs" } | Select-Object Name, DisplayName, RequiredServices
Name DisplayName RequiredServices ---- ----------- ---------------- AppIDSvc Application Identity {RpcSs, CryptSvc, AppID} Appinfo Application Information {RpcSs, ProfSvc} AppVClient Microsoft App-V Client {AppvVfs, RpcSS, AppvStrm, netprofm} AppXSvc AppX Deployment Service (AppXSVC) {rpcss, staterepository} Audiosrv Windows Audio {AudioEndpointBuilder, RpcSs} autotimesvc Cellular Time {rpcss} AxInstSV ActiveX Installer (AxInstSV) {rpcss} BFE Base Filtering Engine {RpcSs} BITS Background Intelligent Transfer Service {RpcSs} BrokerInfrastructure Background Tasks Infrastructure Service {DcomLaunch, RpcSs, RpcEptMapper} ...
As you can see, there are services that depend only on RpcSs, and services, that require some additional services.
Let’s get services, that depend on Remote Procedure Call (RPC) and some other services.
Get-Service | Where-Object { $_.RequiredServices.Name -like "RpcSs" -and $_.RequiredServices.Name -notlike "RpcSs" } | Select-Object Name, DisplayName, RequiredServices
Name DisplayName RequiredServices ---- ----------- ---------------- AppIDSvc Application Identity {RpcSs, CryptSvc, AppID} Appinfo Application Information {RpcSs, ProfSvc} AppVClient Microsoft App-V Client {AppvVfs, RpcSS, AppvStrm, netprofm} AppXSvc AppX Deployment Service (AppXSVC) {rpcss, staterepository} Audiosrv Windows Audio {AudioEndpointBuilder, RpcSs} BrokerInfrastructure Background Tasks Infrastructure Service {DcomLaunch, RpcSs, RpcEptMapper} ...
Now we get the services, that depend on the RpcSs only.
Get-Service | Where-Object { $_.RequiredServices.Name -like "RpcSs" -and -not ($_.RequiredServices.Name -notlike "RpcSs") } | Select-Object Name, DisplayName, RequiredServices
Name DisplayName RequiredServices ---- ----------- ---------------- autotimesvc Cellular Time {rpcss} AxInstSV ActiveX Installer (AxInstSV) {rpcss} BFE Base Filtering Engine {RpcSs} BITS Background Intelligent Transfer Service {RpcSs} ...
So, we can use the -like, -notlike, and -not operators to define rather uncommon conditions.