1

When I use the following command on the powershell console, it works correctly and I see admin command prompt open and the script sudo.bat executes correctly.

sudo.bat contains one line, cd c:\temp\

Start-Process cmd \"/k C:\Users\Varun\Documents\sudo.bat cd /d %cd%\" -Verb RunAs

enter image description here

But below one doesn't work, i.e. when the path has space, the admin command prompt opens but it doesn't run the script file.

Start-Process cmd \"/k C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat cd /d %cd%\" -Verb RunAs

enter image description here

I tried following and it did not work:

Start-Process cmd \"/k \"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat\" cd /d %cd%\" -Verb RunAs

and:

Start-Process cmd \"/k 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat' cd /d %cd%\" -Verb RunAs
2
  • What is the thought process behind changing the current directory to the current directory? Commented Oct 13, 2018 at 10:32
  • Let's say i am at a path c:\temp and it is not admin. I want to have a script that will contain the above line and so i will run that script (env variable), and it will open a Visual Studio Admin command prompt at the same path. I am able to achieve the above without VS command prompt already. Commented Oct 13, 2018 at 10:41

1 Answer 1

3

Use extra quotes. Like so:

Start-Process cmd \"/k ""C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat"" cd /d %cd%\" -Verb RunAs
Sign up to request clarification or add additional context in comments.

3 Comments

one more thing. This changes the current directory to Visual Studio one. Do you think it is possible to return to same location where we wrote the command?
I belive you could declare variable for current path and then use it to change your location.
@VVV: I usually use Push-Location $PSScriptRoot or Push-Location . at first to "temporarily save" the location and Pop-Location at the end of the script :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.