how to put services into array??

I can use this to get which services run with this account:
Get-WmiObject “win32_service” -Filter “StartName=‘domain\user’”

Where I’m stumped is how to put the services into an array so I can loop through the array and stop the services and of course then restart the services.

air code:

$services = do the above into array

for each $service in $services
stop the service

change the domain\user password

for each $service in $services
stop the service

Thank you, Tom

$services = Get-WmiObject "win32_service" -Filter "StartName='domain\\user'"


$services = Get-Service


ForEach ($service in (Get-Services)) {

Alternatively how could I use something like this to get the services??

$Services = Get-WmiObject Win32_Service -ComputerName “.” | Where { $ -eq ‘MSSQLSERVER’ } – this is the general idea

to become something like – this is almost what I need but I don’t know how to construct the where part to properly query the start name of the service(s):

$Services = Get-WmiObject Win32_Service -ComputerName “.” | Where { startname -eq ‘domain\user’ }

Thank you, Tom


$Services = Get-WmiObject Win32_Service -ComputerName "." | Where { $ -eq 'MSSQLSERVER' }

Is a little inefficient.

$Services = Get-WmiObject Win32_Service -ComputerName "." -Filter "Name='MSSQLSERVER'"

Would be more efficient. And you can filter based on StartName the same way. You only use the double backslash when querying the way I’ve done here. If you’re using Where-Object, then you’d use a single backslash. The double backslash is a WMI-specific escape sequence, not needed for filtering at PowerShell itself.

Richard Siddaway wrote a lovely book on WMI and PowerShell, if you plan to mess with this stuff a lot. A lot of good tips and a ton of “how it works.”

Thank you, now I must learn how to change the password for that service account.
The above array can be used to stop and start the services running with that account.
Thank you, Tom

The Win32_Service object has a couple of methods you can use to set the password - the Change() method is the one I’ve used most frequently.

In fact, I think I might have used that exact example in my Toolmaking book.

I saw that somewhere about the Change()!!
I must look for it again. :slight_smile:

Could someone suggest how I fix this request for services??

$services2 = Get-WMIObject Win32_Service -Filter “Name LIKE ‘%SQL%’” AND “StartName=’$account’”

The filter is the problem…I need the AND to narrow down the services.

You could make your life easier and use the Powershell way instead of the WMI way …

Get-CimInstance -ClassName Win32_Service |
Where-Object -FilterScript {_.Name -like '*SQL*' -and _.StartName -eq $account}

The entire filter statement goes inside one set of double quotes.

Thank you everyone, now I better understand the get-ciminstance cmdlet too.
Thank you, Tom