Author Posts

April 24, 2018 at 3:37 pm

Hi everyone,

Can anyone point me to a knowledge article for what the predefined $Function: variable is? I saw some examples on how its used online, but would like to read more about it.

Thanks,
Michael

April 24, 2018 at 5:30 pm

$Function is a reference to the "Function Provider" psdrive (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/providers/function-provider?view=powershell-6)

You can use it to reference functions stored in the session state.

IE.

cls
function test {
    Write-host "This is my function code" -ForegroundColor Green
}

$Function:test

test

Results:

    Write-host "This is my function code" -ForegroundColor Green

This is my function code

Notice how the first output is the code of the function itself, then the second output is the output of the function execution.