Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

im making a script like sudo, the idea is that the sudo function is going to run a scriptblock in elevation. I'm having trouble with the strings variables, that i pass in that scriptblock. Is like the doubles quotes disappear. What is wrong with my approach? Here is the code.

function sud([ScriptBlock]$SomeCode){
$here = Get-Location;
Start-Process Powershell -Verb Runas -ArgumentList "-command &{cd $here; $SomeCode}" 

and i want to run commands like this:

$var ="Path"
$newpath = $env:path + ";c:\somedir"
$target = "Machine"
sud -somecode { [Environment]::SetEnvironmentVariable($var, $newpath, $target)}

for testing purpose one can add the parameter -noexit to the argumentlist..

Start-Process Powershell -Verb Runas -ArgumentList "-noexit","-command &{cd $here; $SomeCode}" 
share|improve this question

Try wrapping the command in single ticks and moving the double quote to the front of the actual command. This is what I do in my environment.

Start-Process Powershell -Verb Runas -ArgumentList `-command "&{cd $here; $SomeCode}"`
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.