Jump to content

Powershell - Help Shortening Command

Oshino Shinobu
Go to solution Solved by Oshino Shinobu,

Alright, managed to answer my own question. Have to do it like this:

 

Get-Service | Where-Object {$_.DisplayName -NotMatch "(User Data Storage_|User Data Access_|Sync Host_|Windows Push Notifications)"}

Hi guys, 

 

So, long story short, I'm modifying a script that we use at work to compare services on servers pre and post reboot so we can revert them.

 

On Server 2016 and 2019, there's a bunch of services that appear to be session dependant. Once you reboot, the services are gone, but the comparison from the script displays them as "different" and wanting to be reverted. 

 

My plan is to just stop our comparison script from selecting these services in the first place. I have the below command working (it's not all the services in question, but the formatting is the main concern here), but I feel like it's needlessly long to have to specify the property -and -notmatch for each one I want to filter. 

 

Wondering if there's a way to shorten this. I feel like

{$_.DisplayName -notmatch ("CDPUserSVC", "Contact Data_", "User Data Storage_")}

should work, but it doesn't. 

 

 

This is the only way I've got it working so far:

get-service | Where-Object {$_.DisplayName -notmatch ("CDPUserSVC_") -and $_.DisplayName -notmatch ("Contact Data_") -and $_.DisplayName -notmatch ("User Data Storage_") -and $_.DisplayName -notmatch ("Windows Push Notification")}
Link to comment
Share on other sites

Link to post
Share on other sites

Alright, managed to answer my own question. Have to do it like this:

 

Get-Service | Where-Object {$_.DisplayName -NotMatch "(User Data Storage_|User Data Access_|Sync Host_|Windows Push Notifications)"}
Link to comment
Share on other sites

Link to post
Share on other sites

On 6/16/2020 at 3:26 AM, Oshino Shinobu said:

Alright, managed to answer my own question. Have to do it like this:

 


Get-Service | Where-Object {$_.DisplayName -NotMatch "(User Data Storage_|User Data Access_|Sync Host_|Windows Push Notifications)"}

Does the | in the notmatch work with other things i.e. -like? That's the first time I've seen that, pretty cool.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, fringie said:

Does the | in the notmatch work with other things i.e. -like? That's the first time I've seen that, pretty cool.

Doesn't look like it. -like and -notlike matches a wildcard string but -match and -notmatch matches regex. I imagine that's why. 

 

I guess if you're going to be using -like multiple times, you can just define it in a function first rather than typing it out each time.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×