How to get the current directory of the cmdlet being executed

后端 未结 17 2286
滥情空心
滥情空心 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:51

    The reliable way to do this is just like you showed $MyInvocation.MyCommand.Path.

    Using relative paths will be based on $pwd, in PowerShell, the current directory for an application, or the current working directory for a .NET API.

    PowerShell v3+:

    Use the automatic variable $PSScriptRoot.

提交回复
热议问题