Switch Logged in user using powershell

SP 61 Reputation points
2021-01-09T01:54:49.94+00:00

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 for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-01-11T13:10:54.793+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.