Display all service names matching a string pattern
You can use Get-Service instead of get-WMIObject and do it like this"
get-service sql* | select -expand name
The easiest way to achieve that is using -Filter
Parameter
Get-WMIObject Win32_Service -Filter "Name LIKE '%SQL%'" | Select -ExpandProperty Name
In case, you want to go with your code only, here is the way you can modify it:
Get-WmiObject Win32_Service | Select-Object -ExpandProperty Name | Select-String -pattern 'SQL'
Edit: LIKE operator takes a few meta characters to support matching pattern.
[]
- for range matching. For example, Name LIKE '[a-f]%'
will list all services starting with any letter from a to f.
^
- not. For example, Name LIKE '[^a-f]%'
will list services that do not start with any letter from a to f.
_
- matches one letter. For example, Name LIKE 'S_L%'
will list services that start with S and followed by any letter.