How can a function get a caller's variable without picking up its own?

November 30, 2026 5 answers
Asked by Tovrali.6z

I have a function that takes a variable name and inspects its value and type. How can it find the version visible to the caller, even if the function has a local variable with the same name? I also want to avoid extra output from the lookup.

Answers

_f9~Tavreno

Get-Variable with an explicit -Scope checks that scope, not the normal chain of visible scopes. So -Scope 1 skips your function's local scope, but it does not by itself search every ancestor. A variable in a more distant scope can be missed.

For a name-based lookup, one option is to check numbered scopes starting at 1 and stop at the first match. You'll need to handle the end of the scope chain and cases where visibility rules prevent access. Test it with nested functions and scripts, especially if modules are involved.

Also, -ValueOnly returns the value, not a variable object with a Scope property. Filtering that value with Where-Object on $_.Scope will not tell you which scope supplied it.

9w+canvasratchet_438:

Starting at 1 also means the nearest matching ancestor wins, rather than collecting several same-named variables and trying to filter them afterward.

Reply to this answer
PipkinHarbor-8

The quoted value '0,1,2,3,4,5,6,7,8,9,Global,Script' is one literal string, not a list of scope arguments. Removing the quotes doesn't make -Scope accept multiple scopes either. It takes one scope identifier at a time, so multiple checks need a loop.

Reply to this answer
r.625~mivquari

Does the function actually need to find the variable by name? If the goal is to inspect its value, passing the value directly is much simpler and avoids collisions inside the function. Pass a label separately if you want to display a name alongside it.

A reference is not required just to inspect a value. It becomes relevant if the function needs to work with the caller's variable itself rather than just the value supplied.

Tovrali.6z:

The name is useful for the display too, but passing it separately from the value could work.

Reply to this answer
4c+Mordexo

If the variable is specifically in the function's script scope, use Get-Variable -Name $varName -Scope Script -ValueOnly. For a fixed name, $script:varName is simpler. Neither is a general substitute for finding the caller-visible variable, since the caller could have a nearer one or be in another script scope.

Reply to this answer
z.721+velkaro

PowerShell functions send uncaptured success-stream output to the caller. Assign the lookup result to a variable, and capture or suppress any other commands that are only there for internal work. If you want the inspection results returned, leave just those results on the success stream.

Don't rely on the last entry in $Error to tell you whether this particular lookup failed. It can contain older errors. Handle the lookup's error directly, and make sure a variable whose value is $null isn't mistaken for a missing variable.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions