Reputation: 6450
So i have this simple function
that return my current script location:
function Get-CurrentDir
{
$MyInvocation.MyCommand.Path
}
And i want to print this, so i try from PowerShell ISE after the function declaration:
Write-Host $(Get-CurrentDir)
Write-Host (Get-CurrentDir)
Write-Host Get-CurrentDir
And this is my output:
Write-Host $(Get-CurrentDir) --> Write-Host $(Get-CurrentDir)
Write-Host (Get-CurrentDir) --> Write-Host (Get-CurrentDir)
Write-Host Get-CurrentDir --> Write-Host Get-CurrentDir
What i am doing wrong ?
Upvotes: 2
Views: 548
Reputation: 72171
Ok, you would need to get the variable from the script scope, like so:
function Get-CurrentDir
{
$script:MyInvocation.MyCommand.Path
}
also this won't work interactively. you would want to read more about_scopes in PowerShell.
Upvotes: 5