I recently discovered that you can get powershell's functions by reference using the modifier $function:
. But I noticed a strange problem...
By convention, POSH uses a {Verb}-{Noun}
convention for function names, where {Verb}
is part of aproved POSH verb names. For instance: Get-Member
or Invoke-WebRequest
.
The thing is, that calling $function:Get-Member
, for example, is not valid because of the hyphen; it works just fine if you declare a function like ShowMessage
and calls: $fn = $function:ShowMessage
. So I'd like to know if there's a way to escape this call.
PS.: I do know of another option, but is much much more verbose:
function Show-Message { Write-Host "Foo"; }
$fn = (Get-Item "function:Show-Message").ScriptBlock;
$fn.Invoke();
Update: Although @PetSerAl was very helpfull and explained the problem, I'll mark @Ansgar Wiechers's response as the answer because it's better documented.
${function:Show-Message}
. Also$function:
is not modifier.function:
is a drive name.${function:Get-Member}
is also not valid, asGet-Member
is not a powershell function, but a cmdlet for compiled .net code fromMicrosoft.PowerShell.Commands.Utility.dll
.$function
was something related to "scope" like$using
and$global
.local
,script
,global
,private
,using
andvariable
, IIRC, anything else interpreted as drive name.