How to get the current directory of the cmdlet bei

2019-01-08 07:00发布

问题:

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 when I execute c:\temp\myscripts\mycmdlet.ps1 which has a settings file at c:\temp\myscripts\settings.xml I would like to be able to store c:\temp\myscripts in a variable within mycmdlet.ps1.

This is one solution which works (although a bit cumbersome):

$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$settingspath = $directorypath + '\settings.xml'

Another one suggested this solution which only works on our test environment:

$settingspath = '.\settings.xml'

I like the latter approach a lot and prefer it to having to parse the filepath as a parameter each time, but I can't get it to work on my development environment. Does anyone have a suggestion on what to do? Does it have something to do with how PowerShell is configured?

回答1:

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.



回答2:

Yes that should work. But if you need to see the absolute path, this is all you need:

(Get-Item -Path ".\").FullName


回答3:

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


回答4:

You can also use:

(Resolve-Path .\).Path

The part in brackets returns a PathInfo object.

(Available since PowerShell 2.0.)



回答5:

Path is often null. This function is safer.

function Get-ScriptDirectory
{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value;
    if($Invocation.PSScriptRoot)
    {
        $Invocation.PSScriptRoot;
    }
    Elseif($Invocation.MyCommand.Path)
    {
        Split-Path $Invocation.MyCommand.Path
    }
    else
    {
        $Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\"));
    }
}


回答6:

Try :

(Get-Location).path

or:

($pwd).path


回答7:

Get-Location will return the current location:

$Currentlocation=Get-Location


回答8:

I like the one line solution :)

$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent


回答9:

Try this:

$WorkingDir = Convert-Path .


回答10:

You would think that using '.\' as the path means that it's the invocation path. But not all the time. Example, if you use it inside a job ScriptBlock. In which case, it might point to %profile%\Documents.



回答11:

To expand on @Cradle 's answer: you could also write a multi-purpose function that will get you the same result per the OP's question:

Function Get-AbsolutePath {

    [CmdletBinding()]
    Param(
        [parameter(
            Mandatory=$false,
            ValueFromPipeline=$true
        )]
        [String]$relativePath=".\"
    )

    if (Test-Path -Path $relativePath) {
        return (Get-Item -Path $relativePath).FullName -replace "\\$", ""
    } else {
        Write-Error -Message "'$relativePath' is not a valid path" -ErrorId 1 -ErrorAction Stop
    }

}


回答12:

In Powershell 3 and above you can simply use

$PSScriptRoot