Reboot & Resume
One of the most powerful features of PoshUI Workflows is the ability to handle system reboots and automatically resume execution.
Key Concept
RequestReboot() is not a system reboot command. It's a workflow control signal that:
- Saves the current workflow state to disk
- Stops the current task execution
- Shows the user a reboot dialog
- Allows the workflow to resume from the next task after reboot
Requesting a Reboot
From within any workflow task, call $PoshUIWorkflow.RequestReboot():
Add-UIWorkflowTask -Step 'Execution' -Name 'UpdateKernel' -Title 'Updating Kernel' -ScriptBlock {
$PoshUIWorkflow.WriteOutput("Kernel update applied. System needs to restart.", "INFO")
# Request a reboot with a reason
$PoshUIWorkflow.RequestReboot("Kernel Update Applied")
# Code after RequestReboot() will execute after resume
$PoshUIWorkflow.WriteOutput("System rebooted successfully", "INFO")
}What Happens
- State Saved: Workflow state is automatically saved to
%LOCALAPPDATA%\PoshUI\PoshUI_Workflow_State.json - Task Marked Complete: Current task is marked as completed
- Dialog Shown: User sees reboot options ("Reboot Now", "Reboot Later", "Simulate")
- Execution Stops: Remaining tasks pause until after reboot
- Script Closes: The PoshUI window closes
Resuming After Reboot
When the system restarts and the user runs the script again, it automatically detects the saved state and resumes.
Basic Resume Pattern
Import-Module PoshUI.Workflow
# 1. Initialize workflow (must match the original)
New-PoshUIWorkflow -Title 'System Setup'
# 2. Check for saved state
if (Test-UIWorkflowState) {
# 3. Load and resume from saved state
Resume-UIWorkflow
# 4. Redefine the SAME tasks (Resume-UIWorkflow marks completed ones to skip)
Add-UIWorkflowTask -Step 'Execution' -Name 'Phase1' -Title 'Phase 1' -ScriptBlock { ... }
Add-UIWorkflowTask -Step 'Execution' -Name 'Phase2' -Title 'Phase 2' -ScriptBlock { ... }
# 5. Show the UI (skips wizard, continues from next pending task)
Show-PoshUIWorkflow
} else {
# First run: normal workflow setup
Add-UIStep -Name 'Config' -Title 'Configuration' -Order 1
Add-UITextBox -Step 'Config' -Name 'ServerName' -Label 'Server Name'
Add-UIStep -Name 'Execution' -Title 'Execution' -Order 2 -Type Workflow
Add-UIWorkflowTask -Step 'Execution' -Name 'Phase1' -Title 'Phase 1' -ScriptBlock { ... }
Add-UIWorkflowTask -Step 'Execution' -Name 'Phase2' -Title 'Phase 2' -ScriptBlock { ... }
Show-PoshUIWorkflow
}Reboot & Resume Checklist
Use this checklist when implementing reboot/resume workflows:
- [ ] Define workflow structure - Create all steps and tasks before first run
- [ ] Call
Test-UIWorkflowState- Check for saved state at script start - [ ] Call
Resume-UIWorkflow- Load saved state if it exists - [ ] Redefine all tasks - Must define the same tasks in the same order
- [ ] Use
RequestReboot()- Call from task when reboot is needed - [ ] Test first run - Verify wizard and first tasks work
- [ ] Test resume - Simulate reboot and verify resume works
- [ ] Verify cleanup - Confirm state file is deleted after completion
- [ ] Test cancellation - Verify state file cleanup if user cancels
State File Management
Location
%LOCALAPPDATA%\PoshUI\PoshUI_Workflow_State.json
Contents
- Current task index (which task to resume from)
- All wizard input values
- Completed task output and progress
- Log file path (for continuity)
- Timestamp and computer info
Automatic Cleanup
The state file is automatically deleted when:
- Workflow completes successfully
- User cancels the workflow
Manual Cleanup
If you need to manually clear the state (for testing or recovery):
Clear-UIWorkflowStateState Persistence Details
What is Saved
- ✅ Wizard input values
- ✅ Completed task status and output
- ✅ Log file path
- ✅ Current task index
What is NOT Saved
- ❌ Variables created during task execution
- ❌ External file changes
- ❌ Registry modifications
- ❌ Installed software
Important: Your tasks should be idempotent (safe to run multiple times). For example:
- Check if software is already installed before installing
- Check if a service is already running before starting it
- Use
-Forceflags cautiously on resume
Example: Idempotent Task
Add-UIWorkflowTask -Step 'Execution' -Name 'InstallFeature' -Title 'Install Windows Feature' `
-ScriptBlock {
$feature = Get-WindowsFeature -Name 'Web-Server'
if ($feature.Installed) {
$PoshUIWorkflow.WriteOutput("Feature already installed", "INFO")
} else {
$PoshUIWorkflow.WriteOutput("Installing feature...", "INFO")
Install-WindowsFeature -Name 'Web-Server'
$PoshUIWorkflow.WriteOutput("Feature installed", "INFO")
}
}Auto-Launch After Reboot (Optional)
For the best user experience, add your script to the Windows RunOnce registry key so it launches automatically after reboot:
$scriptPath = $PSCommandPath
$regPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce'
# Add script to RunOnce (runs once at next logon)
New-ItemProperty -Path $regPath -Name 'PoshUIWorkflow' `
-Value "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`"" `
-Force | Out-Null
# Then request reboot
$PoshUIWorkflow.RequestReboot("System reboot required")Troubleshooting
State file not found after reboot
- Verify the script is running as the same user
- Check that
%LOCALAPPDATA%path is accessible - Confirm
Test-UIWorkflowStateis called beforeResume-UIWorkflow
Tasks running again after resume
- Ensure you call
Resume-UIWorkflowbefore defining tasks - Verify task names match exactly (case-sensitive)
- Check that task order hasn't changed
State file not deleted after completion
- Verify workflow completed without errors
- Check that all tasks have status "Completed"
- Manually clear with
Clear-UIWorkflowStateif needed
Next: Workflow Logging | Working with Tasks
