Start-Process -WorkingDirectory as administrator does not set location

后端 未结 4 688
攒了一身酷
攒了一身酷 2021-02-18 16:58

When I enter the command

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

it opens new PowerShell window with D:\\folder

相关标签:
4条回答
  • 2021-02-18 17:19

    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

    0 讨论(0)
  • 2021-02-18 17:34

    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.

    0 讨论(0)
  • 2021-02-18 17:36

    I just ran your code example and it opened correctly for me at the WorkingDirectory location. Ensure the directory exists before you run the command. I tested against a drive on C and secondary drive as well and both worked.

    0 讨论(0)
  • 2021-02-18 17:37

    Once you run Powershell as administrator;

    user the push-location command like so:

    Push-Location -Path C:\
    

    or put it into your script and run the script from the elevated Powershell prompt.

    0 讨论(0)
提交回复
热议问题