ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Use powershell to create a scheduled task to reboot computers on schedule

    IT Discussion
    powershell reboots users
    4
    10
    12.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch
      last edited by JaredBusch

      This thread reminded me I needed to update my process for forcibly rebooting user computers.

      As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.

      This should be pretty self explanatory.
      Create variables with the pieces of the command.
      Then use the register command to create the task.

      #remove old task
      Unregister-ScheduledTask -TaskName "Weekly Reboot" -Confirm:$false
      # Create task action
      $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force'
      # Create a trigger (Mondays at 4 AM)
      $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am
      # The user to run the task
      $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount
      # The name of the scheduled task.
      $taskName = "Weekly Reboot"
      # Describe the scheduled task.
      $description = "Forcibly reboot the computer at 4am on Mondays"
      # Register the scheduled task
      Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
      

      Once run, it will look like this:
      611c5fc1-47a5-4a2a-a904-ebf287ee14f3-image.png

      1 J 2 Replies Last reply Reply Quote 4
      • 1
        1337 @JaredBusch
        last edited by

        @jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:

        This thread reminded me I needed to update my process for forcibly rebooting user computers.

        As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.

        This should be pretty self explanatory.
        Create variables with the pieces of the command.
        Then use the register command to create the task.

        # Create task action
        $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force'
        # Create a trigger (Mondays at 4 AM)
        $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am
        # The user to run the task
        $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount
        # The name of the scheduled task.
        $taskName = "Weekly Reboot"
        # Describe the scheduled task.
        $description = "Forcibly reboot the computer at 4am on Mondays"
        # Register the scheduled task
        Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
        

        Good idea!

        Maybe you should add a test to check if the task is already created?

        That way you could use the same script to modify the time or whatever you wanted to do. Without having several tasks created I mean.

        JaredBuschJ 1 Reply Last reply Reply Quote 1
        • JaredBuschJ
          JaredBusch @1337
          last edited by

          @pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:

          @jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:

          This thread reminded me I needed to update my process for forcibly rebooting user computers.

          As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.

          This should be pretty self explanatory.
          Create variables with the pieces of the command.
          Then use the register command to create the task.

          # Create task action
          $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force'
          # Create a trigger (Mondays at 4 AM)
          $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am
          # The user to run the task
          $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount
          # The name of the scheduled task.
          $taskName = "Weekly Reboot"
          # Describe the scheduled task.
          $description = "Forcibly reboot the computer at 4am on Mondays"
          # Register the scheduled task
          Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
          

          Good idea!

          Maybe you should add a test to check if the task is already created?

          That way you could use the same script to modify the time or whatever you wanted to do. Without having several tasks created I mean.

          I added a remove

          1 2 Replies Last reply Reply Quote 0
          • 1
            1337 @JaredBusch
            last edited by 1337

            @jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:

            @pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:

            @jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:

            This thread reminded me I needed to update my process for forcibly rebooting user computers.

            As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.

            This should be pretty self explanatory.
            Create variables with the pieces of the command.
            Then use the register command to create the task.

            # Create task action
            $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force'
            # Create a trigger (Mondays at 4 AM)
            $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am
            # The user to run the task
            $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount
            # The name of the scheduled task.
            $taskName = "Weekly Reboot"
            # Describe the scheduled task.
            $description = "Forcibly reboot the computer at 4am on Mondays"
            # Register the scheduled task
            Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
            

            Good idea!

            Maybe you should add a test to check if the task is already created?

            That way you could use the same script to modify the time or whatever you wanted to do. Without having several tasks created I mean.

            I added a remove

            Remove if it's already created? That works.

            EDIT: Yeah, I see it now. Unregister-ScheduledTask.

            1 Reply Last reply Reply Quote 0
            • 1
              1337 @JaredBusch
              last edited by

              @jaredbusch

              For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.

              That way I know the tasks in there is not generated by something else.

              JaredBuschJ ObsolesceO 2 Replies Last reply Reply Quote 0
              • JaredBuschJ
                JaredBusch @1337
                last edited by JaredBusch

                @pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:

                @jaredbusch

                For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.

                That way I know the tasks in there is not generated by something else.

                But they did not do that for everything.
                These tasks are all in the root \ path.
                4a14fb76-77a3-4e53-bb9d-4caed04b9964-image.png

                1 1 Reply Last reply Reply Quote 0
                • ObsolesceO
                  Obsolesce @1337
                  last edited by

                  @pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:

                  @jaredbusch

                  For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.

                  That way I know the tasks in there is not generated by something else.

                  One could also fix the description to say the source of the task. I get the appeal of placing them in their own folder nested somewhere in that mess, but it's so much easier to deal with them in the top level directory.

                  1 1 Reply Last reply Reply Quote 0
                  • 1
                    1337 @JaredBusch
                    last edited by

                    @jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:

                    @pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:

                    @jaredbusch

                    For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.

                    That way I know the tasks in there is not generated by something else.

                    But they did not do that for everything.
                    These tasks are all in the root \ path.
                    4a14fb76-77a3-4e53-bb9d-4caed04b9964-image.png

                    Yes, it's strange. Not consistent. But there are MANY more tasks under the Microsoft folder and it's sub folders.

                    1 Reply Last reply Reply Quote 0
                    • 1
                      1337 @Obsolesce
                      last edited by 1337

                      @obsolesce said in Use powershell to create a scheduled task to reboot computers on schedule:

                      @pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:

                      @jaredbusch

                      For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.

                      That way I know the tasks in there is not generated by something else.

                      One could also fix the description to say the source of the task. I get the appeal of placing them in their own folder nested somewhere in that mess, but it's so much easier to deal with them in the top level directory.

                      I guess you could fix the name. But it's mostly misbehaving apps that put tasks in the top level directory. I mean Microsoft added the folder structure for task organization. It wasn't there in the old days.

                      But yeah, it works regardless.

                      1 Reply Last reply Reply Quote 0
                      • J
                        JasGot @JaredBusch
                        last edited by

                        This post is deleted!
                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post