Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting a listing of all temp variables in the current session

21 views
Skip to first unread message

Marco Shaw

unread,
Nov 23, 2006, 3:33:33 PM11/23/06
to
I'll keep a PSH session open for a while as I learn...

I'll come back and use $a or $a+=[something] after having used it hours,days
ago...

Is there a way to list/clear all variables in a running "session"? I
realize I can just exit and start, but is there something cooler I can run?

Marco


William Stacey [C# MVP]

unread,
Nov 23, 2006, 4:02:51 PM11/23/06
to
have a look at remove-variable (i.e. remove-variable * -whatif) You should
be able to script something up for your needs.

--
William Stacey [C# MVP]

"Marco Shaw" <ma...@Znbnet.nb.ca> wrote in message
news:ux4lm6zD...@TK2MSFTNGP04.phx.gbl...

Keith Hill [MVP]

unread,
Nov 23, 2006, 4:14:42 PM11/23/06
to
"Marco Shaw" <ma...@Znbnet.nb.ca> wrote in message
news:ux4lm6zD...@TK2MSFTNGP04.phx.gbl...

Try "gci variable:" to list all PoSH variables. Unfortunately it doesn't
sort by name so I create a function for this I call vars:

function vars { get-variable | sort Name }

As William mentions, you can use Remove-Variable to eliminate variables.
The problem is nowing which you can remove and which you can't. Some will be
created with options that won't allow you to remove them (well at least
without using -force).

--
Keith

Andrew Watt [MVP]

unread,
Nov 23, 2006, 5:06:27 PM11/23/06
to
Marco,

If you routinely use some prefix for your own variable names e.g. My
or XX then you could use

get-childitem variable:My* | remove-item -whatif

or similar to check what you would delete.

Remove the -whatif when you're comfortable that you will delete the
desried variables.

Andrew Watt MVP

On Thu, 23 Nov 2006 16:33:33 -0400, "Marco Shaw" <ma...@Znbnet.nb.ca>
wrote:

Roman Kuzmin

unread,
Nov 23, 2006, 6:00:40 PM11/23/06
to
I use this:

# remove some variables
Get-Variable * -Scope global | &{process{
if (!$_.Description -and !$_.Option -and !$_.Attributes) {
Remove-Variable $_.Name -Scope global -ErrorAction SilentlyContinue
}
}}

# up to you
$Error.Clear()

--
Thanks,
Roman


Jeffrey Snover [MSFT]

unread,
Nov 23, 2006, 11:10:49 PM11/23/06
to
Now you are ready for Start-NewScope (this is SO awesome)
http://blogs.msdn.com/powershell/archive/2006/04/25/583268.aspx

What I do is to start a new scope and do my experimentation. When I exit
that scope - all my temp variables get cleaned up.
I think immediately start a new session.

Now (put your seatbelt on for this one). If you ever wonder what your
temporary variables are, you just start ANOTHER nested scope and do the
following command:
Compare-Object (Get-Variable -scope 1) (get-Variable -Scope 0) -property
Name

Let me show you (I use the alias SANS for Start-NewScope):
PS> sans
Starting New Scope
PS> $x=55
PS> $y="test"
PS> sans
Starting New Scope
PS> Compare-Object (get-Variable -scope 1) (Get-Variable -scope 0) -property
name

name SideIndicator
---- -------------
x <=
y <=


Just to be clear about what is going on - Get-Object takes a -SCOPE
parameter. 0 means the current scope, 1 means the parent scope, 2 means the
grandparent etc. The reason you want to do a diff is that certain variables
get copied to a scope every time a new scope is created.
--
Jeffrey Snover [MSFT]
Windows PowerShell Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.


dreeschkind

unread,
Nov 24, 2006, 4:43:02 AM11/24/06
to
Strange, for some reason I can't use Vim inside a nested prompt.
It does start fine and the icon of the console window turns from PowerShell
to Vim (just like from a normal PowerShell session).
Also the keyboard controls Vim now (i.e. I can type [esc]:q! to exit Vim).
However, I can't see what I'm typing and I also don't see any output from
Vim itself (line numbers etc.). The window still shows the PowerShell screen
content.
When I exit Vim, I return to the nested PowerShell prompt and get the
following error message:

vim.bat : Vim: Warnung: Die Ausgabe erfolgt nicht auf einem Terminal
At line:1 char:3

#translation:
#Warning: Output does not take place on a terminal

Any ideas?

--
greetings
dreeschkind

Maximilian Hänel

unread,
Nov 24, 2006, 8:24:56 AM11/24/06
to
Hi Jeffrey,

> Now (put your seatbelt on for this one). If you ever wonder what your
> temporary variables are, you just start ANOTHER nested scope and do the
> following command:
> Compare-Object (Get-Variable -scope 1) (get-Variable -Scope 0) -property
> Name

Wow, that's cool! But what the heck I'm doing wrong?

0:20 > $host.EnterNestedPrompt()
1:20 > $a=3
1:21 > Compare-Object (get-variable -scope 1) (get-variable -scope 0)
Get-Variable : Die Bereichsnummer "1" übersteigt die Anzahl der aktiven
Bereiche.
Parametername: scopeID
Der tatsächliche Wert war 1.
Bei Zeile:1 Zeichen:29
+ Compare-Object (get-variable <<<< -scope 1) (get-variable -scope 0)
-property Name

I can't access scope 1. Why?

tia

Max

Jeffrey Snover [MSFT]

unread,
Nov 24, 2006, 12:19:39 PM11/24/06
to
> Wow, that's cool! But what the heck I'm doing wrong?
>
> 0:20 > $host.EnterNestedPrompt()
> 1:20 > $a=3

As yes - the thing that creates the new scope is NOT
$host.EnterNestedPromp() - it is the FUNCTION (Start-NewScope) that contains
that line. That one bit me as well.

dreeschkind

unread,
Nov 24, 2006, 12:46:02 PM11/24/06
to
Btw. edit.com works as expected inside the nested prompt.
Probably some bug in Vim. Can anyone else confirm this?

--
greetings
dreeschkind

Maximilian Hänel

unread,
Nov 24, 2006, 4:35:14 PM11/24/06
to
Hi Jeffrey,

> As yes - the thing that creates the new scope is NOT
> $host.EnterNestedPromp() - it is the FUNCTION (Start-NewScope) that
> contains that line.

Argh! Good point...

> That one bit me as well.

I'm glad to hear ;-)

Thanks,

Max

0 new messages