question

SP-4916 avatar image
0 Votes"
SP-4916 asked IanXue-MSFT answered

Switch Logged in user using powershell

Logging in as a Admin, I'm creating a Scheduled Task to be run as a User. The task when fired is run in background. However I need it to be run in foreground. I read that if the same user is logged in, Task Scheduler will run the same task in foreground. Even though Admin is logged in, using powershell, can we trick the scheduler that the user has logged in?

I tried changing below keys in register editor, but no luck.

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData\2\LoggedOnUser

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData\2\LoggedOnSAMUser

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnUser

windows-server-powershellwindows-server-2019
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

You can create a scheduled task of powershell. Add %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe to the "Program/script" field and –NoProfile –ExecutionPolicy Bypass –File X:\path\task.ps1 to the "Add arguments" field. The task.ps1 can be like below. I suppose you want to run notepad

 $user='XXXXX'
 $password='XXXXX'
 $securePassword= ConvertTo-SecureString $password -AsPlainText -Force
 $credential = New-Object System.Management.Automation.PSCredential $user, $securePassword
 Start-Process -FilePath 'Notepad.exe' -ArgumentList 'C:\temp\a.txt' -Credential $credential

Best Regards,
Ian
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.