# how to put services into array??

**URL:** <https://forums.powershell.org/t/how-to-put-services-into-array/10163>\
**Category:** PowerShell Help\
**Created:** [February 19, 2018, 3:49pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163 "2018-02-19T15:49:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![tommls](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tommls](https://forums.powershell.org/u/tommls)\
**Post date:** [February 19, 2018, 3:49pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/1 "2018-02-19T15:49:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 19, 2018, 3:54pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/2 "2018-02-19T15:54:52Z")

</div>

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

Or

```
$services = Get-Service
```

Or

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

---

<div class="post-metadata">

**Author:** ![tommls](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tommls](https://forums.powershell.org/u/tommls)\
**Post date:** [February 19, 2018, 3:58pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/3 "2018-02-19T15:58:39Z")

</div>

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

$Services = Get-WmiObject Win32\_Service -ComputerName “.” | Where { $\_.name -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

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 19, 2018, 4:01pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/4 "2018-02-19T16:01:27Z")

</div>

This:

```
$Services = Get-WmiObject Win32_Service -ComputerName "." | Where { $_.name -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.”

---

<div class="post-metadata">

**Author:** ![tommls](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tommls](https://forums.powershell.org/u/tommls)\
**Post date:** [February 19, 2018, 4:06pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/5 "2018-02-19T16:06:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 19, 2018, 4:07pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/6 "2018-02-19T16:07:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 19, 2018, 4:07pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/7 "2018-02-19T16:07:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tommls](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tommls](https://forums.powershell.org/u/tommls)\
**Post date:** [February 19, 2018, 4:09pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/8 "2018-02-19T16:09:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tommls](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tommls](https://forums.powershell.org/u/tommls)\
**Post date:** [February 19, 2018, 6:26pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/9 "2018-02-19T18:26:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 19, 2018, 7:16pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/10 "2018-02-19T19:16:14Z")

</div>

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}
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 19, 2018, 7:34pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/11 "2018-02-19T19:34:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tommls](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tommls](https://forums.powershell.org/u/tommls)\
**Post date:** [February 20, 2018, 8:42am UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/12 "2018-02-20T08:42:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:36pm UTC](https://forums.powershell.org/t/how-to-put-services-into-array/10163/13 "2024-05-16T20:36:20Z")

</div>

Closed on May 16, 2024, 8:36pm UTC


