Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start-Process -WorkingDirectory as administrator does not set location

Tags:

When I enter the command

Start-Process powershell -WorkingDirectory "D:\folder" 

it opens new PowerShell window with D:\folder location set.

But when I enter the command

Start-Process powershell -WorkingDirectory "D:\folder" -Verb RunAs 

it opens new PowerShell window with admin rights but with C:\Windows\system32 location set.

How can I open new PowerShell window with admin rights and my own location determined?

like image 897
Paweł Avatar asked Apr 19 '17 11:04

Paweł


2 Answers

I also had the same problem and solved it with this command:

Start-Process powershell.exe -verb runAs -ArgumentList '-NoExit', '-Command', 'cd D:\folder' 

Once you run the above command, Windows will launch with admin authority and the specified directory.

like image 102
ふゆな Avatar answered Sep 22 '22 17:09

ふゆな


Here's another example which can be used for opening CMD from PowerShell as an administrator into the current folder:

Start-Process cmd -ArgumentList ("/k cd {0}" -f (Get-Location).path) -Verb RunAs
if used within a script you can use
Start-Process cmd -ArgumentList ("/k cd {0}" -f $PSScriptRoot) -Verb RunAs

If you want to open a new elevated PowerShell session from the current one which is not elevated you can use:
Start-Process powershell.exe -ArgumentList ("-NoExit",("cd {0}" -f (Get-Location).path)) -Verb RunAs
or
Start-Process powershell.exe -ArgumentList ("-NoExit",("cd {0}" -f (Get-Location).path)) -Verb RunAs
when used inside scripts

like image 36
Kiril Nedev Avatar answered Sep 23 '22 17:09

Kiril Nedev