Get Services but filter out lines that match lines from a text file

I’ve been asked to create powershell to report back any services on remote computers set to Automatic but Not running.

It returns lines of services shouldn’t be running, how do I exclude those lines?

My initial idea was to use text file that contains list of exclusions I’m not interested in, but how would I incorporate that into the command?

Would it be better to output the initial output to a file, then parse that output to remove lines I want excluding.

I have the command to get the services and output to a file…it’s the filtering out the rubbish I’m stuck with.

I’m very new to PS but willing to learn so any basic commands to get me started would be great.

Welcome to the forum. :wave:t4:


With a filter condition. Usually Where-Object.

It depends pretty much on how you want to do it. You can use an input file and import it with Get-Content for example. Or you simply have it in your script.


Cool. Please show it to the world! :wink:

We all started once. :wink: But it is beyond the scope of a forum like this to teach you basic PowerShell. You should always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them. Then - if you get stuck - it’s best to ask a specific question about a particular piece of code you wrote. And for that you should share this piece of code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

Regardless of all that - in the vast majority of the cases you’re not the very first one with a given task. So it is pretty likely that you can find some helpful examples you can adapt to your particlar requirements. Please do at least your basic research before you come to forum and ask for help.

Thanks in advance again.

Thanks for the response, apologies for the lack of info.

Here is the command.

get-content -Path serverlist.txt | % {invoke-command -ComputerName $_ -Command { get-service | where {($_.starttype -eq 'Automatic') -and ($_.status -ne 'Running') -and ($_.DisplayName -notlike '*google update*') -and ($_.DisplayName -notlike '*Microsoft Edge*')}}} | Select-Object -Property PSComputerName,DisplayName,Name| Export-Csv -Path output.csv

I’m not sure the reason for the % just before the Invoke command, the rest I understand.

That command could get very long with exclusions and I would like to get clever in the future to exclude specific services on specific servers. But that’s for another day…

I see two options, import an array from text file or use get-content…replace the -notlike entries with either of those two…assume an array would be quicker as it’s in memory and not reading a file every time.

I’ll have a little play to see what I can come up with.

I’ve been batch scripting for many years and fine with that, but I need to move away and onto PS, but my old brain keeps saying “How would I do this in batch script” lol

Thanks again for your response, no doubt be back soon with even less hair than I already have.

Great, B U T :

You should format your code nicely to make it easier to read and therefore easier to maintain. AND … since Invoke-Command can take an array of computernames for its parameter -ScriptBlock wich alias is -command you don’t actually need a loop.

Please always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them

The % is an alias for the cmdlet Foreach-Object and it is common sense not to use aliasses in scripts. They are fine on the console but they do make your code harder to read. The same is valid for where instead of Where-Object or select instead of Select-Object … and with using tab completion it does not even requires a lot of typing. :wink:
You can list all existing aliasses on you system with

I’d say it depends on the amount of exceptions you want to add and on the way you’re providing them. If you use the correct names for them you could use an array and the operator -contains or ‘-in’ or actually -notcontains and -notin to compare the services found against the list you provide.

Here you can read more about:

This is understandable but often misleading I think. I’d like to encourage you to think outside of the box and in case search for inspiration online before you implement a solution you a kind of translated from batch to PowerShell. In the vast majority of the cases you’re not the first one with a given task and you can find a lot of examples for almost every existing problem online.

Here’s how I would tweak your code to run faster and look better:

$ComputerNameList = Get-Content -Path serverlist.txt
Invoke-Command -ComputerName $ComputerNameList -ScriptBlock { 
    Get-Service | 
    Where-Object { 
            ($_.starttype -eq 'Automatic') -and 
            ($_.status -ne 'Running') -and 
            ($_.DisplayName -notlike '*google update*') -and 
            ($_.DisplayName -notlike '*Microsoft Edge*') 
} | 
Select-Object -Property PSComputerName, DisplayName, Name | 
Export-Csv -Path output.csv
1 Like

Thanks for this, the formatting makes so much sense and very easy to read.

Really appreciate your detailed response.

I may need to add a check if the server is on line and exclude from the service running checks or at least export those to a failure output.

Hopefully I’ll build on this and maybe when I have a list of services that should be running, use this format to search for and start those services.

This code runs quicker than I expected, will give it a full test on all the servers to see what run time I get.

Checking your profile “Tending to be lazy - that’s why I love to write PS scripts” I’ve always said, if I have to do it 5 or more times, script it…it’s something I try to push within the team.

Thanks again for your response, really appreciated!