Category filter

Script to restart Windows devices

Regularly restarting a Windows device ensures optimal performance by enabling the completion of essential updates and installations, finalizing security patches and driver integrations. It resolves memory leaks by completely clearing the system’s volatile Random Access Memory (RAM). A restart can also terminate frozen or stuck background processes and services, release file locks and restore core system functions.

Deploying custom scripts to remotely restart managed devices provides administrators with granular control beyond standard system commands. Using the Execute Custom Script action in Hexnode UEM, you can fully customize the restart experience.

Specifically, these scripts allow for a delayed reboot or a custom warning message displayed to the user beforehand. Additionally, they can retrieve critical audit data, such as the last restart time and the identity of the user who initiated the action. This article provides both Batch and PowerShell scripts to facilitate these workflows.

Supported Versions


The script is supported for execution on the following Windows versions:

  • Windows 10 v1803+
  • Windows 10 v1703 to Windows 10 v1709 (if .NET Framework v4.7.1+ is installed on the device).
  • Windows 11 (Pro, Enterprise, Education)

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Executing custom script via Hexnode UEM – Quick steps

  1. Navigate to the Manage tab in your Hexnode UEM console and select your Windows device.
  2. From Actions, select Execute Custom Script from Deployments.

    The Execute Custom Script action is found under the “deployments” subsection of the Remote actions for the Windows device.

  3. Upload the .ps1 PowerShell script and click Execute.
  4. The script output is available via Action History > Execute Custom Script action > Status > Show Output.
    Navigating to the Show Output button in the Action History tab to verify the results of a scheduled Windows restart script.

Batch scripts

  1. Restart device after a time delay

    When deploying the script via the Execute Custom Script action, enter the desired restart delay (in minutes) as a numerical value within the Arguments field; this automatically triggers a warning message on the device to notify the user of the imminent restart.

    The execution logs for a scheduled Windows restart are displayed within the Show Output window to verify the script's success.

  2. Restart device immediately

    Execute the script below to restart the device immediately.

    The execution logs for an immediate Windows Restart action are displayed within the Show Output window on the Hexnode console to confirm the script's success.

  3. Check last restart time of device

    Script returns the date and time of the last restart of the endpoint device to the Show Output window in the UEM console.
    The last restart timestamp for Windows device shown within the Show Output screen.

PowerShell scripts

  1. Restart device

    Execute the following command to restart the device immediately.

    The execution logs for scheduled Windows restart via PowerShell are displayed in the Hexnode Show Output window to verify task registration and script success.

  2. Schedule device restart

    To schedule restart on a Windows device, run the following script:

    The script triggers, $Trigger, an action $Action at a specific time and frequency set by the administrator deploying the script. The Action is the execution of the script file restart.ps1.The command to restart the device is given under $ScriptContent, which is saved to the restart.ps1 script file. The restart.ps1 script file is saved to the location specified by the admin under $ScriptPath.

    The scheduler script will use the path of restart.ps1 provided to trigger the restart action with the highest privilege.

    The administrator must also configure the frequency to run the script (Once, Daily, Weekly etc) in $Trigger replacing -Daily in the script and the specific time (in 24 hour format) to execute the scheduler script, following –At.

  3. Find the user who last restarted the device

    On running the script, the name of the user who last restarted the remote device is returned to the Hexnode UEM console in the Show Output window.
    The details of the user who last restarted the device as obtained from the Show Output window using a Windows PowerShell script.

  4. Check last restart and uptime of device

    Script returns the date and time of the last restart and the uptime of the endpoint device to the UEM console in the Show Output window.
    The Hexnode console displays a PowerShell script output showing the Windows device's last restart time and uptime.

  5. Get Windows restart audit report

    The Windows restart log is retrieved using the PowerShell Get-WinEvent command, which provides a detailed audit trail of every restart since the device was enrolled in Hexnode. This log identifies the specific date and time of each event along with the name of the user or system process that initiated the restart.
    The Hexnode Show Output window displays a comprehensive restart log retrieved via a PowerShell script, listing the timestamps and initiating users for each restart event on the Windows device.

    Admins can access the comprehensive list directly through the Show Output window to review the full history of system restart events.
    The Hexnode console displays the Windows device restart history report, showing a chronological list of system restart times as retrieved by the PowerShell script.

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

Sample Script Repository