How to get the current directory of the cmdlet being executed

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

    The easiest method seems to be to use the following predefined variable:

     $PSScriptRoot
    

    about_Automatic_Variables and about_Scripts both state:

    In PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in PowerShell 3.0, it is valid in all scripts.

    I use it like this:

     $MyFileName = "data.txt"
     $filebase = Join-Path $PSScriptRoot $MyFileName
    

提交回复
热议问题