How to get the current directory of the cmdlet being executed

后端 未结 17 2216
滥情空心
滥情空心 2020-11-29 17:03

This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance,

17条回答
  •  离开以前
    2020-11-29 17:49

    this function will set the prompt location to script path, dealing with the differents way to get scriptpath between vscode, psise and pwd :

    function Set-CurrentLocation
    {
        $currentPath = $PSScriptRoot                                                                                                     # AzureDevOps, Powershell
        if (!$currentPath) { $currentPath = Split-Path $pseditor.GetEditorContext().CurrentFile.Path -ErrorAction SilentlyContinue }     # VSCode
        if (!$currentPath) { $currentPath = Split-Path $psISE.CurrentFile.FullPath -ErrorAction SilentlyContinue }                       # PsISE
    
        if ($currentPath) { Set-Location $currentPath }
    }
    

提交回复
热议问题