0

I'm trying to do something like this:

function Copy-Item( [string]$source, [string]$destination )
{
    $global:Copy-Item $source -destination $destination
}

But I can't figure out what is the correct syntax for the global Copy-Item invokation. Is this even possible?

Simone
  • 11,655
  • 1
  • 30
  • 43
  • 1
    Possible duplicate of [PowerShell - How do I call a cmdlet in a function when overriding that cmdlet's name with the same name as the function?](https://stackoverflow.com/questions/5556651/powershell-how-do-i-call-a-cmdlet-in-a-function-when-overriding-that-cmdlets). Syntax-wise, `$global::(Copy-Item)` parses, but I don't know I'd advocate that over disambiguating the module name -- you're still not sure what you get with that. – Jeroen Mostert Aug 22 '19 at 10:46
  • The linked post covers _cmdlet_ invocation, which solves your problem. Note that if you truly wanted to call a _function_ in the global scope, as the question title suggests, e.g., `Get-Foo`, the syntax would be `global:Get-Foo ....`; i.e., simply remove the `$`. – mklement0 Aug 22 '19 at 13:08
  • @mklement0 any idea why if I use global:Get-Foo I receive a `CallDepthOverflow` error? It seems that it is invoking itself. – Simone Aug 22 '19 at 14:47
  • 1
    That would imply that the `Get-Foo` function you're calling _from_ is itself already in the global scope. The approach only works if it is in a different scope. – mklement0 Aug 22 '19 at 14:48

0 Answers0