title: PowerShell script running as different user in Windows Task Scheduler
body: <p>I am trying to configure a PowerShell script which will run in Windows Task Scheduler for multiple service accounts.  I found a way to run the PowerShell script with a given user via the 2nd answer in this link <a href="https://stackoverflow.com/questions/28989750/running-powershell-as-another-user-and-launching-a-script">Running PowerShell as another user, and launching a script</a>.</p>
  
  <p>For reference this is the PowerShell snippet:</p>
  
  <pre><code>$username = 'userA'
  $password = 'passwordA'
  
  $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
  $credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
  Start-Process C:\BatchPath\MyBatch.bat -Credential $credential
  </code></pre>
  
  <p>Now in Windows Task Scheduler I configure the job and in the execution I have it setup to run Powershell.exe with additional arguments '-ExecutionPolicy Bypass C:\ScriptPath\Script.ps1'.  If the user for this task scheduler entry is configured as userA then it works. If I configure the task scheduler entry on userB then it fails.  Both userA and userB are administrators on the machine.</p>
  
  <p>In the second scenario I would expect that the script file would be started by userB but then the PowerShell Start-Process would force the batch file to be run as userA.  From watching Task Manager I don't see the job within the batch file started.</p>
  
  <p>This example is a bit superficial but in the final form of the PowerShell script it would be running different batch files with different service accounts.</p>