Unregister-ScheduledTask -TaskName "MediaServer" -Confirm:$false -ErrorAction SilentlyContinue # Get the media-server directory (parent of scripts folder) $serverRoot = (Get-Item $PSScriptRoot).Parent.FullName # Find Python executable $pythonPath = (Get-Command python -ErrorAction SilentlyContinue).Source if (-not $pythonPath) { Write-Error "Python not found in PATH. Please ensure Python is installed and accessible." exit 1 } $action = New-ScheduledTaskAction -Execute $pythonPath -Argument "-m media_server.main" -WorkingDirectory $serverRoot $trigger = New-ScheduledTaskTrigger -AtStartup $principal = New-ScheduledTaskPrincipal -UserId "$env:USERNAME" -LogonType S4U -RunLevel Highest $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable Register-ScheduledTask -TaskName "MediaServer" -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Description "Media Server for Home Assistant" Write-Host "Scheduled task 'MediaServer' created with working directory: $serverRoot"