Just wrap the remove-variable in an if block to check if its one of the ones 
you want to save.

Sent from my Windows Phone
________________________________
From: Mote, Todd<mailto:[email protected]>
Sent: ‎2/‎26/‎2016 3:53 PM
To: [email protected]<mailto:[email protected]>
Subject: RE: [powershell] clear session variables without closing the session?

Hmm.  Well I got it to work, but it works a little too well.  it takes out my 
$xaml and $window variables too, which kills the UI.  Hehe.  I guess it needs 
to leave out $xaml and $window…

From: [email protected] [mailto:[email protected]] On 
Behalf Of Scott Crawford
Sent: Friday, February 26, 2016 3:32 PM
To: [email protected]
Subject: RE: [powershell] clear session variables without closing the session?

Depending how much planning ahead you want to do, you could run:

$DefaultVariables = (Get-Variable).Name

at the beginning of your session instead of in the snippet I sent. Then my 
snippet would be faster and wouldn’t need to be adjusted based on the 
interpreter you’re using.

From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Scott Crawford
Sent: Friday, February 26, 2016 2:59 PM
To: [email protected]<mailto:[email protected]>
Subject: RE: [powershell] clear session variables without closing the session?

This seems to work from the console. Some tweaking would be necessary to work 
from ISE or whatever interpreter you’re using.

$AllVariables = (Get-Variable).Name
$DefaultVariables = (powershell {Get-Variable}).Name
$ComparedVariables = Compare-Object $AllVariables $DefaultVariables
foreach ($ComparedVariable in $ComparedVariables) {
    Remove-Variable $ComparedVariable.InputObject
}
Remove-Variable ComparedVariable
Remove-Variable ComparedVariables



From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Mote, Todd
Sent: Friday, February 26, 2016 2:54 PM
To: '[email protected]' 
<[email protected]<mailto:[email protected]>>
Subject: RE: [powershell] clear session variables without closing the session?

I designed this UI for others to use and made a reset button to clear all the 
text boxes and pull downs to get it ready to go again, so while I don’t mind to 
run it in a new session every time, I was looking for something the reset 
button could do from my UI to clean things up?

From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Ryan
Sent: Friday, February 26, 2016 2:48 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [powershell] clear session variables without closing the session?

I use my extension to run PowerShell scripts in a new session:
http://www.ephingadmin.com/run-wpf-powershell-scripts-in-the-ise-without-all-the-crashing/

It basically saves what you have in the console (either saving the script or 
saving the unsaved script to a temp file) and then runs that file in a new 
session outside of the ISE. I wrote this for WPF writing since the ISE crashes 
every now and then when editing WPF scripts.



On Fri, Feb 26, 2016 at 2:45 PM Mote, Todd 
<[email protected]<mailto:[email protected]>> wrote:
Is there an easy or clever way to clear all of the user defined variables I 
define without having to close my UI, or list each of them explicitly for 
remove-variable?  I have a reset button in my UI to clear everything out for 
the next run of this process, but it seems like there’s always something left 
behind that screws something up on a subsequent run.

Todd

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to