user979033
user979033

Reputation: 6450

How to print return value of function

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

Answers (1)

4c74356b41
4c74356b41

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

Related Questions