mahesh mahesh - 3 months ago 9
PowerShell Question

Filter exactly matched windows services using PowerShell

I'm trying to filter out the windows services using PowerShell

Following are the windows services that we have:

DataGenerator_1_UAT
DataGenerator_2_UAT
DataGenerator_3_UAT
DataGenerator_1_Blue_UAT
DataGenerator_2_Blue_UAT
DataGenerator_1_Green_UAT
DataGenerator_2_Green_UAT


If I do DataGenerator_*_UAT
it is giving complete service list. But I want only following three.

DataGenerator_1_UAT
DataGenerator_2_UAT
DataGenerator_3_UAT


and If I give
DataGenerator_*_Blue_UAT

or
DataGenerator_*_Green_UAT
, it is giving correct result.

is there any way we can filter out these services using PowerShell Regex ?
Can someone please suggest me how do I filter.

Thanks in Advance.

Kel Kel
Answer

Looks like all service names actually match DataGenerator_*_UAT pattern (in names like DataGenerator_1_Blue_UAT substring "1_Blue" matches *, too).

You can use Where-Object filter with a regexp. Assuming that by * you mean anything except underscores (_):

Get-Service | Where-Object { $_.Name -match "^DataGenerator_[^_]+_UAT$" }