Last active
July 22, 2025 02:55
-
-
Save andyg2/33258eb27dd7d3643bc7c893e66a1318 to your computer and use it in GitHub Desktop.
Tested on Windows Server 2025 Datacenter Azure edition in Azure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the task details | |
$taskName = "StartDockerDesktopAtBoot" | |
$taskDescription = "Starts Docker Desktop automatically after boot (with 2-minute delay), regardless of user login." | |
$wrapperScriptPath = [Environment]::GetFolderPath("Desktop") + "\DockerStartWrapper.ps1" # Wrapper script on current user's desktop for delay | |
try { | |
# Get current user's full username for the principal | |
$userId = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name | |
# Check if the task already exists and remove it to avoid conflicts | |
$existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue | |
if ($existingTask) { | |
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false | |
Write-Host "Existing task removed to allow recreation." | |
} | |
# Create a wrapper script for the 2-minute delay (workaround for -Delay parameter not being supported) | |
$wrapperContent = @" | |
# start service... | |
echo "Waiting for 2 minutes before starting Docker Desktop..." | |
Start-Sleep -Seconds 120 | |
echo "Starting service com.docker.service... " | |
start-service -Name com.docker.service | |
echo "Starting docker desktop... " | |
start "$Env:Programfiles\Docker\Docker\Docker Desktop.exe" | |
echo "Done." | |
"@ | |
$wrapperContent | Out-File -FilePath $wrapperScriptPath -Encoding UTF8 | |
Write-Host "Wrapper script created at $wrapperScriptPath for delay handling." | |
# Create the action to run the wrapper script via PowerShell | |
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$wrapperScriptPath`"" | |
# Create the trigger for at startup (without -Delay to avoid errors) | |
$trigger = New-ScheduledTaskTrigger -AtStartup | |
# Create the principal: Run as current user with highest privileges | |
$principal = New-ScheduledTaskPrincipal -UserId $userId -LogonType S4U -RunLevel Highest | |
# Create settings: Allow running on battery, no execution time limit, etc. | |
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries:$true -DontStopIfGoingOnBatteries:$true -ExecutionTimeLimit ([TimeSpan]::Zero) -Hidden:$false -StartWhenAvailable:$true -Compatibility Win8 | |
# Create the task object | |
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Description $taskDescription | |
# Register the task in Task Scheduler (provide password here) | |
Register-ScheduledTask -TaskName $taskName -InputObject $task | |
# Output confirmation | |
Write-Host "Scheduled task '$taskName' created successfully. It will start Docker Desktop at boot (after a 2-minute delay via wrapper) with highest privileges, as user '$userId'." | |
} catch { | |
Write-Host "Error creating task: $($_.Exception.Message)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By fancybear-dev from this comment.
Tested on Windows Server 2025 Datacenter Azure edition in Azure.