title: Run code block locally as a different user in powershell script
body: <p>This is something incredibly simple, but I just can't get anything to work. I want to run a block code in a powershell script under a specific user. The keyword is <em>locally</em> and I'm using powershell 2.0.</p>
  
  <p>Invoke-Command seems to require a remote host? I run the following and the error message that I see seems to suggest as much:</p>
  
  <pre><code>$strScriptUser = "DOMAIN\USER"
  $strPass = "PASSWERD"
  $PSS = ConvertTo-SecureString $strPass -AsPlainText -Force
  $cred = new-object system.management.automation.PSCredential $strScriptUser,$PSS
  Invoke-Command -ComputerName "." -scriptblock {
  write-output "HI!"
  } -Credential $cred
  </code></pre>
  
  <p>Start-Job with -ScriptBlock isn't supported with powershell 2.0? I run the following and the error message that I see seems to suggest as much:</p>
  
  <pre><code>$strScriptUser = "DOMAIN\USER"
  $strPass = "PASSWERD"
  $PSS = ConvertTo-SecureString $strPass -AsPlainText -Force
  $cred = new-object system.management.automation.PSCredential $strScriptUser,$PSS
  Start-Job -ScriptBlock {
  write-output "HI!"
  } -Credential $cred
  </code></pre>
  
  <p>Am I doing something wrong, or is there an alternative way?</p>
  
  <p>Added: Here is what I'm trying to do in the first place. I'm making a scheduled task that runs when a user logs into/unlocks a terminal that writes logon information to a file. The scheduled task runs as the local user in order to get at the username, profile, etc. information. The logon information is then written to a log file using a different user account, which is the only account that can modify the file. To deter access to the logon credentials in the script I convert the script to an EXE using PS2EXE.</p>