Skip to content

Instantly share code, notes, and snippets.

@andyg2
Last active July 22, 2025 02:55
Show Gist options
  • Save andyg2/33258eb27dd7d3643bc7c893e66a1318 to your computer and use it in GitHub Desktop.
Save andyg2/33258eb27dd7d3643bc7c893e66a1318 to your computer and use it in GitHub Desktop.
Tested on Windows Server 2025 Datacenter Azure edition in Azure.
# 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)"
}
@andyg2
Copy link
Author

andyg2 commented Jul 22, 2025

By fancybear-dev from this comment.

Tested on Windows Server 2025 Datacenter Azure edition in Azure.

Install Docker Desktop (choose WSL2!) and go through all the steps until you can start containers.
    Fun fact, Docker Desktop is officially not supported AT ALL on Windows Server. I have not seen or experienced any issues, but don't go barking at Docker when we are CLEARLY running unsupported setups.
Use the magic ps1 script
    It's designed to be idem-potent for quick iterating
    Does not require any password on-disk, but does not run as SYSTEM...
    The 2 minute wait is dirty, it's designed to not overload the OS at boot and supersede any other potential dependencies
    Downside, Docker Desktop UI is not retrievable after this. Don't know why, also don't care - but if you do, beware. To update Docker Desktop, I recommend Docker Desktop CLI (docker desktop update) or winget (winget upgrade --all --accept-package-agreements --accept-source-agreements)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment