# 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)" }