Skip to content

Instantly share code, notes, and snippets.

@tahir-hassan
Last active August 23, 2017 14:45
Show Gist options
  • Save tahir-hassan/906f2deea0494560d2756c3c44303d81 to your computer and use it in GitHub Desktop.
Save tahir-hassan/906f2deea0494560d2756c3c44303d81 to your computer and use it in GitHub Desktop.
PowerShell wrapper for runas.exe (Start-UserProcess)
# this does not support all the features of runas.exe, but is a first stab
Function Start-UserProcess {
param([Parameter(Mandatory=$true)][string]$Username, [switch]$SaveCredentials, [Parameter(Mandatory=$true)][string]$Program, [string[]]$ArgumentList)
$program = & {
if ($ArgumentList) {
((& { $Program; $ArgumentList }) | foreach { "\`"$_\`"" }) -join " "
} else {
"`"$Program`"";
}
}
$saveCred = if ($SaveCredentials) { "/savecred" } else { "" }
runas.exe "/user:$Username" $saveCred $program
}
# examples:
# Start-UserProcess -Username "MyDomain\MyUsername" -SaveCredentials -Program "notepad" -ArgumentList "C:\Temp\myfile.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment