0

This is my powershell function:

function A
{
  $a = "securestring"
  return $a
}

$b = A
Remove-Variable -Name b -Scope "Local" 

$b is no longer in memory by now. But what about $a?

3
  • 2
    AFAIK also no longer in memory as runs in the function scope, and is cleared once function has finished running. Commented Jul 19, 2017 at 21:09
  • "$b is no longer in memory by now." - how do you know that? The .Net Garbage Collector appears to be implementation dependent so you have no guarantee when it will run or not. If the word choice securestring is a hint that you need to clear physical memory for some important security purpose, it going out of scope of a PowerShell function does not do that. Commented Jul 19, 2017 at 23:14
  • Garbage collection and Remove-Variable or Clear-variable is a different thing I presume? Commented Jul 20, 2017 at 18:31

1 Answer 1

1

The easy answer: no. After it exits that scope, its variables are gone. Also, if you want $B to capture the output of the function, you should really do something like this instead:

Function A
{
    "securestring"
}
$B = A
> $B
> securestring
> $B.GetType().Name
> String
Remove-Variable -Name B -Scope 'Local'
> $B
> $B.GetType()
> You cannot call a method on a null-valued expression.

Your example is a bit convoluted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.