Forum Discussion
Immediate Restart from Intune
Hy,
you can not trigger a Restart, Reboot command directly via Platform Script or Remediation.
rahuljindal Kindly Reminder "
- Don't put reboot commands in detection or remediations scripts."
- https://learn.microsoft.com/en-us/intune/intune-service/fundamentals/remediations
I use this as a Platform Script in order to Rename Device in or after ESP or immediately after User Sign In.
Bonus Function 😉
Place the log if you want under "C:\ProgramData\Microsoft\IntuneManagementExtension\YourLogName" and adjust the time from the Task to your needs.
# Define the log name (custom for this script)
$LogName = "add log Name at your choice"
function Write-Log {
param (
[string]$LogName,
[string]$Message,
[ValidateSet("Info", "Success", "Error")]
[string]$LogType = "Info"
)
$LogDirectory = "add your Path here" # Define the path where logs will be stored
# Create the log directory if it doesn't exist
if (-not (Test-Path -Path $LogDirectory)) {
New-Item -ItemType Directory -Path $LogDirectory -Force | Out-Null
}
# Create the log file name
$LogFileName = "$LogName.log"
$LogFilePath = Join-Path -Path $LogDirectory -ChildPath $LogFileName
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$FormattedMessage = "[$Timestamp] [$LogType] $Message"
Add-Content -Path $LogFilePath -Value $FormattedMessage
switch ($LogType) {
"Info" { Write-Host $FormattedMessage -ForegroundColor White }
"Success" { Write-Host $FormattedMessage -ForegroundColor Green }
"Error" { Write-Host $FormattedMessage -ForegroundColor Red }
}
}
# Check if the scheduled task already exists
$taskName = "Name the task as you like"
$taskExists = Get-ScheduledTask | Where-Object { $_.TaskName -eq $taskName }
if (-not $taskExists) {
try {
Write-Log -LogName $LogName -Message "Creating scheduled task: $taskName" -LogType "Info"
Write-Host "Creating scheduled task: $taskName" -ForegroundColor Yellow
# Create the scheduled task
$STaction = New-ScheduledTaskAction -Execute 'c:\windows\system32\shutdown.exe' -Argument '-r -t 0'
$STtrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(45) # Trigger Task 45 minutes from first run
$STSet = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
$STuser = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName $taskName -TaskPath "\" -Action $STaction -Settings $STSet -Trigger $STtrigger -Principal $STuser
Write-Log -LogName $LogName -Message "Scheduled task $taskName created successfully." -LogType "Success"
}
catch {
Write-Log -LogName $LogName -Message "Error creating scheduled task: $($_.Exception.Message)" -LogType "Error"
Write-Host "Error creating scheduled task: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
}
else {
Write-Log -LogName $LogName -Message "Scheduled task $taskName already exists. Skipping creation." -LogType "Info"
Write-Host "Scheduled task $taskName already exists. Skipping creation." -ForegroundColor Yellow
}
# Wait for the task to execute
Write-Log -LogName $LogName -Message "Waiting for the scheduled task to execute..." -LogType "Info"
Write-Host "Waiting for the scheduled task to execute..." -ForegroundColor Yellow
Start-Sleep -Seconds 20 # Wait for 20 seconds to ensure the task has time to execute
Good luck!
- rahuljindalJul 17, 2025Bronze Contributor
Bogdan_Guinea why not? It is perfectly fine to use remediation for this as it supports scheduling of a task. Now should you use it, it will completely dependent on requirements.
- Bogdan_GuineaJul 18, 2025Iron Contributor
I meant that no direct reboot, restart via DR or platform script works.
Of course you can use a task for that, that's why I shared my PS to create a task.
Good luck!