Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Get-Service is very slow to return results

Tags:

powershell

I have tried several methods for gathering service data and I can't seem to get one to meet all my needs. Get-Service works fine but is very slow when I pipe in a couple Where-Object properties. Get-CimInstance is much faster but I can't figure out how to exclude services. Any ideas?

Here's my code attempts so far. This one is fast until I add the Where-Object. Then it takes 3 times longer if I do:

Get-Service -DisplayName * -ComputerName $Name -Exclude $ExcludedServices | Where-Object { $_.status -eq 'Running' -or $_.StartType -eq 'Automatic' }

This one works much faster but I don't know how to exclude a list of Services if needed:

Get-CimInstance -ClassName Win32_Service -ComputerName $Name | Where-Object { $_.state -eq 'Running' -or $_.StartMode -eq 'Auto' }
like image 555
TSchwa Avatar asked Oct 14 '25 18:10

TSchwa


1 Answers

I don't know how to exclude a list of Services if needed

Get-CimInstance allows you to impose a WQL WHERE clause constraint on the query:

Get-CimInstance Win32_Service -Filter 'Name != "excludedSvc"'

You can also restrict the items based on the State or StartMode properties inside the query, so the remote computer doesn't have to send back all of the services:

Get-CimInstance Win32_Service -Filter 'Name != "excludedSvc" AND State = "Running" AND StartMode = "Auto"'
like image 119
Mathias R. Jessen Avatar answered Oct 17 '25 09:10

Mathias R. Jessen