How do I dynamically create functions that are accessible in a parent scope? How do I dynamically create functions that are accessible in a parent scope? powershell powershell

How do I dynamically create functions that are accessible in a parent scope?


Another option would be to use the Set-Item -Path function:global:ChildFunction -Value {...}

Using Set-Item, you can pass either a string or a script block to value for the function's definition.


The other solutions are better answers to the specific question. That said, it's good to learn the most general way to create global variables:

# inner scopeSet-Variable -name DynFEx -value 'function DynF() {"Hello DynF"}' -scope global# somewhere other scopeInvoke-Expression $dynfexDynF

Read 'help about_Scopes' for tons more info.


You can scope the function with the global keyword:

function global:DynF {...}