How to Create and Run a Scheduled Task as Currently Logged-in User in Windows 10


2 views

Many developers need to run scheduled tasks that interact with the user interface or require the logged-in user's environment variables. Windows 10 changed the task scheduler interface, removing the "Run only when user is logged on" option that was previously available in Windows 7.

The most reliable method is to use PowerShell to create the task with proper user context:


# Create a scheduled task that runs as current user
$Action = New-ScheduledTaskAction -Execute "notepad.exe"
$Trigger = New-ScheduledTaskTrigger -AtLogon
$Settings = New-ScheduledTaskSettingsSet -RunOnlyIfLoggedOn
$User = "$env:USERDOMAIN\$env:USERNAME"

Register-ScheduledTask -TaskName "MyUserTask" -Action $Action -Trigger $Trigger -Settings $Settings -User $User

For batch operations or systems without PowerShell:


schtasks /create /tn "MyUserTask" /tr "notepad.exe" /sc ONLOGON /ru "%USERDOMAIN%\%USERNAME%" /rl HIGHEST
  • Always store passwords securely if required
  • Consider using the highest privilege level needed
  • Test tasks thoroughly in a non-production environment

If the task fails to run, check these areas:


# View last task result:
Get-ScheduledTask -TaskName "MyUserTask" | Get-ScheduledTaskInfo

Common solutions include:

  • Granting "Log on as batch job" rights
  • Ensuring the user has proper permissions
  • Verifying the executable path is correct

For tasks that need full UI access:


$Action = New-ScheduledTaskAction -Execute "notepad.exe"
$Trigger = New-ScheduledTaskTrigger -AtLogon
$Settings = New-ScheduledTaskSettingsSet -RunOnlyIfLoggedOn -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$Principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType Interactive

Register-ScheduledTask -TaskName "InteractiveTask" -Action $Action -Trigger $Trigger -Settings $Settings -Principal $Principal

When migrating from Windows 7 to Windows 10, many developers noticed the disappearance of the "Run only when user is logged on" group option in Task Scheduler. This becomes particularly problematic when you need scripts to interact with the user session (like GUI applications or user-specific environment variables).

Here's how to create such a task programmatically using PowerShell:


# Define task parameters
$taskName = "MyUserTask"
$action = New-ScheduledTaskAction -Execute "notepad.exe"
$principal = New-ScheduledTaskPrincipal -UserId (Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty UserName) -LogonType Interactive
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries

# Create and register the task
Register-ScheduledTask -TaskName $taskName -Action $action -Principal $principal -Settings $settings

For batch scripts or legacy systems, you can use the command-line utility:


schtasks /create /tn "MyUserTask" /tr "notepad.exe" /sc DAILY /st 09:00 /ru "%USERNAME%" /it

The /it flag is crucial - it makes the task run only when the user is logged on interactively.

When working with scheduled tasks as logged-in users:

  • Never store passwords in scripts - Windows will prompt for credentials during task creation
  • Use -LogonType Interactive for GUI applications but Service for background tasks
  • Consider UAC limitations - elevated tasks may still prompt for confirmation

If your task isn't running as expected:


# Check last run result
Get-ScheduledTask -TaskName "MyUserTask" | Get-ScheduledTaskInfo

# View all tasks for current user
Get-ScheduledTask | Where-Object { $_.Principal.UserId -eq $env:USERNAME }