Author Posts

April 5, 2016 at 4:12 pm

I've been playing around with New-PSSession for several computerNames and setting them up in a variable ($DCs)

I can retrieve those Sessions fine with Get-Sessions and then, wanted to remove those sessions altogether (close and then free up the variable) so I used Remove-PSSessions $DCs. It seemed to work.

But when I repeat the New-PSSession for the same variable, I get a whole new set of session IDs (incremented sequentially)

So, then I thought I would just clear and/or remove the variable $DCs altogether and that doesn't seem to work. I get no error with with either clear-variable $DCs or remove-variable $DCs yet, $DCs persists within my PS ISE admin console.

1) I wish to generate new PS-Sessions using the same earlier session IDs (helps me understand PSSession)
2) I wish to understand why I can't get rid of a manually defined variable name that persists (get-variable $DCs) without closing the shell.


April 5, 2016 at 4:39 pm

When you run scripts by pressing the "Run" button (or pressing F5) in the ISE, it behaves a lot like you had dot-sourced a script at the powershell.exe prompt. Variables in the script's top scope will persist in the global session.

You'll probably wind up with new session IDs every time you call New-PSSession; it shouldn't matter if they match whatever you had on a previous execution.

April 5, 2016 at 6:06 pm

I was using ISE's lower console, not running a script. Just running one liners in the book essentially and I like doing it in ISE

April 5, 2016 at 6:21 pm

Ah. Same thing, then. If you assign something to a variable at the console, then it's going to stick around.

April 5, 2016 at 6:42 pm

thanks Dave: noted.