Start-Process -WorkingDirectory as administrator does not set location

后端 未结 4 681
攒了一身酷
攒了一身酷 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

提交回复
热议问题