joop s
joop s

Reputation: 107

powershell memory space of variable after function return

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?

Upvotes: 0

Views: 544

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

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.

Upvotes: 1

Related Questions