title: How do I execute a PS1 file as a different user?
body: <p>I need to be able to run a PowerShell script remotely (via Jenkins) as a different user. Since it will be executed as a Jenkins job, <code>Get-Credential</code> is not an option for me. Below is the script I have created but it simply does not work.</p>
  
  <pre><code>$uname='domain\username'
  $pwd='password'
  $passw=Convertto-SecureString -String $pwd -AsPlainText -force
  $mycred=New-object -TypeName System.Management.Automation.PSCredential -ArgumentList $uname, $passw
  
  Invoke-Command -FilePath "C:\test_scripts\fetchquery.ps1" -Authentication default -Credential $mycred -computername localhost
  </code></pre>