# disable and enable services

**URL:** <https://forums.powershell.org/t/disable-and-enable-services/9996>\
**Category:** PowerShell Help\
**Created:** [January 19, 2018, 5:29am UTC](https://forums.powershell.org/t/disable-and-enable-services/9996 "2018-01-19T05:29:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![roger-ohlsson](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@roger-ohlsson](https://forums.powershell.org/u/roger-ohlsson)\
**Post date:** [January 19, 2018, 5:29am UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/1 "2018-01-19T05:29:38Z")

</div>

Hi. im trying to find a solution for our servers (windows 2008R2) when they are going to be pached we have to login to ech server and enable windows update services and start it. I think that this can be done through a invoke-comand script…

Have tried with following scripts Next issue is - After Pathed is done is should disable and stop the services

```
Get-content "E:\temp\srv.txt" | foreach {set-Service -ComputerName $_ -Name wuauserv -status Stopped | -StartupType Disabled -PassThru}

=============================================================================================
## Script 2 After Pathed is done is should disable and stop the services##
$Serverlist = Get-Content E:\Temp\srv.txt
$service = "wuauserv"
Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Stop-Service -Force}
Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Set-Service -StartupType disabled}
Get-Service $service | Set-Service -StartupType disabled}

## Script that enable services and start it ##
$Serverlist = Get-Content E:\Temp\srv.txt
$service = "wuauserv"
Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Set-Service -StartupType Manual}
Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Start-Service}
```

---

<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:** [January 19, 2018, 10:22am UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/2 "2018-01-19T10:22:14Z")

</div>

It would be faster to use New-PSSession to create a bunch of session objects against $ServerList. You can then use those sessions with Invoke-Command instead of -ComputerName. Re-using those sessions is a lot quicker than spinning up new sessions each time. See, “Secrets of PowerShell Remoting” on our ebooks menu.

Variables inside the { script block } **are evaluated on the remote machine**. The remote machine won’t know what $service contains, because that variable only lives on your local computer. You would use $using:service instead, to transfer the value from your local machine to the remote run space. See [https://devops-collective-inc.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/remote-variables.html](https://devops-collective-inc.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/remote-variables.html).

I don’t understand what “After Pathed is done” means, though, I’m sorry.

---

<div class="post-metadata">

**Author:** ![roger-ohlsson](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@roger-ohlsson](https://forums.powershell.org/u/roger-ohlsson)\
**Post date:** [January 19, 2018, 1:21pm UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/3 "2018-01-19T13:21:10Z")

</div>

Hmm sorry for bad english, AfterPatched is done. I trying to say that after the servers are patched and done the wuauserv must be stopped and set in disabled.

---

<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:** [January 19, 2018, 1:25pm UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/4 "2018-01-19T13:25:38Z")

</div>

So, there’s no easy or straightforward way of detecting when the installation is complete. It all depends on the patch being installed. You may have to loop every 5 minutes, for example, and query something like Win32\_QuickFixEngineering to see if the hot fix ID shows as installed or not.

---

<div class="post-metadata">

**Author:** ![roger-ohlsson](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@roger-ohlsson](https://forums.powershell.org/u/roger-ohlsson)\
**Post date:** [January 19, 2018, 1:58pm UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/5 "2018-01-19T13:58:58Z")

</div>

We use BatchPatch for patching all servers remotely and it works really good.  
Just need to get this wuauserv up and kicking before and i dont want to login on each machine for this.

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [January 19, 2018, 2:26pm UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/6 "2018-01-19T14:26:03Z")

</div>

You’ve got the code, where are you having issues?

I’d create a .ps1 file with the following code before patching

```
#getservice
$service = Get-Service -name wuauserv

#set startup type
$service | set-service -startuptype Manual

#startservice
$service | start-service
```

then run

```
invoke-command -computername $serverlist -filepath C:\scripts\startservices.ps1
```

and to stop them

```
#getservice
$service = Get-Service -name wuauserv

#stop service
$service | stop-service

#set startup type
$service | set-service -startuptype Disabled
```

Then run

```
invoke-command -computername $serverlist -filepath C:\scripts\stopservices.ps1
```

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [January 19, 2018, 2:31pm UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/7 "2018-01-19T14:31:17Z")

</div>

Looks like you can even do it with batch patch!

> **[Remotely Starting and Stopping Services on Numerous Computers | BatchPatch -...](https://batchpatch.com/remotely-starting-and-stopping-services-on-numerous-computers)**
>
> Remotely Starting and Stopping Services on Numerous Computers

---

<div class="post-metadata">

**Author:** ![roger-ohlsson](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@roger-ohlsson](https://forums.powershell.org/u/roger-ohlsson)\
**Post date:** [January 19, 2018, 4:01pm UTC](https://forums.powershell.org/t/disable-and-enable-services/9996/8 "2018-01-19T16:01:28Z")

</div>

Really nice work Jon!! 🙂 works like charm

Yeh seems to work in BatchPatch also, will try to add it in on to my Patch Jobs

---

<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/disable-and-enable-services/9996/9 "2024-05-16T20:36:52Z")

</div>


