Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alastairtree/d1da3c888cf25899cc30d3be72c60a0e to your computer and use it in GitHub Desktop.
Save alastairtree/d1da3c888cf25899cc30d3be72c60a0e to your computer and use it in GitHub Desktop.
$DemoUser = 'Domain\username'
$DemoPass = 'complexpassword'
$Command = "ECHO 'Hello world'; WhoAmi;"
$StdOutLogFile = "$env:windir\temp\PowerShellElevatedStdOut.log"
$SecurePassword = ConvertTo-SecureString $DemoPass -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential $DemoUser, $securePassword
"Running '$Command' as current user"
Start-Process Powershell.exe -NoNewWindow -ArgumentList "-Command $Command" -RedirectStandardOutput $StdOutLogFile -Wait
Get-Content -Path $StdOutLogFile #Retrieve the output from the external process
"Running '$Command' as $DemoUser"
Start-Process Powershell.exe -NoNewWindow -ArgumentList "-Command $Command" -RedirectStandardOutput $StdOutLogFile -Wait -Credential $Credential -WorkingDirectory 'C:\Windows\System32'
Get-Content -Path $StdOutLogFile #Retrieve the output from the external process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment