# Automatically Shutdown All Computer After Working Hours

**URL:** <https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391>\
**Category:** PowerShell Help\
**Created:** [September 11, 2022, 5:26pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391 "2022-09-11T17:26:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Partyzant](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@Partyzant](https://forums.powershell.org/u/Partyzant)\
**Post date:** [September 11, 2022, 5:26pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391/1 "2022-09-11T17:26:31Z")

</div>

Hi, I need a hint for a script that every day, for example, at 20 o’clock will ping and shut down all enabled computers from the domain.  
So far I have something like this, admittedly I don’t know if it’s right, but I would still like to add to it this specific time at which the script should work.

//here are the lines with the ping that work and dump the result to a file  
$s = Get-Content -Path ./Domain01.txt  
$c = Get-Credential -Credential domain\Admin  
Stop-Computer -ComputerName $s -Force -Credential $c

---

<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:** [September 11, 2022, 9:58pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391/2 "2022-09-11T21:58:25Z")

</div>

Partyzant,  
Welcome to the forum. 👋🏽

> [@Partyzant](#):
>
> but I would still like to add to it this specific time at which the script should work.

I’m not sure if I get what you mean with that. If you want to run this script at a certain time you should run it at a certain time. 🤷🏽‍♂️ If you don’t want to run it manually you can use the task scheduler. I’d recommend to run this task already with the proper account. Then you don’t need to use it inside the script. 😉

---

<div class="post-metadata">

**Author:** ![Partyzant](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@Partyzant](https://forums.powershell.org/u/Partyzant)\
**Post date:** [September 11, 2022, 10:08pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391/3 "2022-09-11T22:08:22Z")

</div>

but its ok?  
$s = Get-Content -Path ./Domain01.txt  
Stop-Computer -ComputerName $s -Force -Credential $c

---

<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:** [September 11, 2022, 10:10pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391/4 "2022-09-11T22:10:55Z")

</div>

> [@Partyzant](#):
>
> but its ok?

But 2 or 3 test computers to your `./Domain01.txt` and test it! … I’d actually expect it to be `.\Domain01.txt` instead (with a backslash instead a slash) but since PowerShell is very forgiving it should workl this way as well. 😉

---

<div class="post-metadata">

**Author:** ![Partyzant](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@Partyzant](https://forums.powershell.org/u/Partyzant)\
**Post date:** [September 11, 2022, 10:13pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391/5 "2022-09-11T22:13:33Z")

</div>

thank you, I’m just getting started with powershell

---

<div class="post-metadata">

**Author:** ![PoshDad](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/poshdad/32/2626_2.png) [@PoshDad](https://forums.powershell.org/u/PoshDad)\
**Post date:** [September 12, 2022, 11:44pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391/6 "2022-09-12T23:44:59Z")

</div>

> [@Partyzant](#):
>
> e $s -Force -Credential $c

I agree with Olaf, I would use the account with elevated access to run the task.  
Here is a good starter template

```auto
#Choose your version of Powershell running the task, if you need to find your version you can open Powershell and type $PSVersionTable
$Posh7Dir = "C:\Program Files\PowerShell\7\pwsh.exe"
$Posh5Dir = "C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe"

$Action = New-ScheduledTaskAction -Execute $Posh7Dir -Argument "Stop-Computer -ComputerName (Get-Content -Path ./Domain01.txt) -Force"
$Trigger = New-ScheduledTaskTrigger -Daily -At #Put time here
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger 
Register-ScheduledTask -TaskName "YourTaskName" -InputObject $Task

```

---

<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:16pm UTC](https://forums.powershell.org/t/automatically-shutdown-all-computer-after-working-hours/20391/7 "2024-05-16T20:16:40Z")

</div>

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


