Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Setting environments variables

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 08-04-2007   #1 (permalink)
Duncan Smith
Guest


 

Setting environments variables

I need to loop though all env vars and set their value to "" (to clear
the environment before setting a new one). But I can't get ps to
accept a variable substitution for the name?

gci env: |% { $env:$_Name=$_.Value }

Unexpected token '_Name' in expression or statement.
At line:1 char:26
+ gci env: |% { $env:$_Name= <<<< $_.Value }

Thanks,

Duncan


My System SpecsSystem Spec
Old 08-04-2007   #2 (permalink)
Duncan Smith
Guest


 

Re: Setting environments variables

On Aug 4, 9:41 am, Duncan Smith <DSmith1...@googlemail.com> wrote:
> I need to loop though all env vars and set their value to "" (to clear
> the environment before setting a new one). But I can't get ps to
> accept a variable substitution for the name?
>
> gci env: |% { $env:$_Name=$_.Value }
>
> Unexpected token '_Name' in expression or statement.
> At line:1 char:26
> + gci env: |% { $env:$_Name= <<<< $_.Value }
>


Aha, needs the set-item cmdlet...

gci env: |% {$str = [string]$_.Name; set-item -path env:$str ""}

My System SpecsSystem Spec
Old 08-04-2007   #3 (permalink)
Duncan Smith
Guest


 

Re: Setting environments variables


>
> Aha, needs the set-item cmdlet...
>
> gci env: |% {$str = [string]$_.Name; set-item -path env:$str ""}


....and putting it all together (just in case anyone needs to push and
pop environments) we get:

# initialize events hashtable for pushing/popping environments
$envs=@{}

function pushenv
{
# create a new hashtable of the current environment
gci env: |% {$thisenv=@{}}{$thisenv[$_.Name]=$_.Value}

# add this hashtable to the main hashtable
$envs[$envs.count] = $thisenv # add current env to envs
}

function popenv
{
# clear all vars from the current env
gci env: |% { &{param($name,$value) set-item -path env:$name
$value} $_.Name "" }

# populate current env vars from the last pushed env
$envs[$envs.count-1] |% { $_.keys |% { &{param($name,$value) set-
item -path env:$name $value -ea silentlycontinue} $_
$envs[$envs.count-1].$_ }}

# remove last pushed env from the hashtable
$envs.Remove($envs.count-1)
}

pushenv


My System SpecsSystem Spec
Old 08-04-2007   #4 (permalink)
Oisin Grehan
Guest


 

Re: Setting environments variables

On Aug 4, 4:41 am, Duncan Smith <DSmith1...@googlemail.com> wrote:
> I need to loop though all env vars and set their value to "" (to clear
> the environment before setting a new one). But I can't get ps to
> accept a variable substitution for the name?
>
> gci env: |% { $env:$_Name=$_.Value }
>
> Unexpected token '_Name' in expression or statement.
> At line:1 char:26
> + gci env: |% { $env:$_Name= <<<< $_.Value }
>
> Thanks,
>
> Duncan


Environment variables are held in a provider, like most things in
Powershell, so you can just treat it as such:

PS> del env:*

It's not always about the pipeline ;-)

- Oisin

My System SpecsSystem Spec
Old 08-04-2007   #5 (permalink)
Oisin Grehan
Guest


 

Re: Setting environments variables

On Aug 4, 8:03 am, Duncan Smith <DSmith1...@googlemail.com> wrote:
> > Aha, needs the set-item cmdlet...

>
> > gci env: |% {$str = [string]$_.Name; set-item -path env:$str ""}

>
> ...and putting it all together (just in case anyone needs to push and
> pop environments) we get:
>
> # initialize events hashtable for pushing/popping environments
> $envs=@{}
>
> function pushenv
> {
> # create a new hashtable of the current environment
> gci env: |% {$thisenv=@{}}{$thisenv[$_.Name]=$_.Value}
>
> # add this hashtable to the main hashtable
> $envs[$envs.count] = $thisenv # add current env to envs
>
> }
>
> function popenv
> {
> # clear all vars from the current env
> gci env: |% { &{param($name,$value) set-item -path env:$name
> $value} $_.Name "" }
>
> # populate current env vars from the last pushed env
> $envs[$envs.count-1] |% { $_.keys |% { &{param($name,$value) set-
> item -path env:$name $value -ea silentlycontinue} $_
> $envs[$envs.count-1].$_ }}
>
> # remove last pushed env from the hashtable
> $envs.Remove($envs.count-1)
>
> }
>
> pushenv


Alternatively, try:

"push"

$vars = gci env:

"pop"

$vars | % { set-item "env:\$($_.key)" $_.value }

;-)

- Oisin

My System SpecsSystem Spec
Old 08-05-2007   #6 (permalink)
Duncan Smith
Guest


 

Re: Setting environments variables

On Aug 4, 4:38 pm, Oisin Grehan <ois...@gmail.com> wrote:
> On Aug 4, 4:41 am, Duncan Smith <DSmith1...@googlemail.com> wrote:
>
> > I need to loop though all env vars and set their value to "" (to clear
> > the environment before setting a new one). But I can't get ps to
> > accept a variable substitution for the name?

>
> > gci env: |% { $env:$_Name=$_.Value }

>
> > Unexpected token '_Name' in expression or statement.
> > At line:1 char:26
> > + gci env: |% { $env:$_Name= <<<< $_.Value }

>
> > Thanks,

>
> > Duncan

>
> Environment variables are held in a provider, like most things in
> Powershell, so you can just treat it as such:
>
> PS> del env:*
>
> It's not always about the pipeline ;-)
>
> - Oisin


A good point! I'll re-factor it for the sake of brevity.

Another question: Have you seen the CustomClass samples for Payette's
book (using the PSCustomObject ETS system)? I'm using it well, but
can't find a way to specify a constructor so that it gets called
automatically on object instantiation.

Closest I have is something like:

customclass axis {
note axisType 0
note pt @($pt)

method axis {
".ctor called"
}
}

.. $axis = newc axis
.. $axis.axis() # <- must call manually

Not very satisfying...

Thanks,

Duncan


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting java class variables mmiked8162 Vista General 2 03-27-2008 11:25 AM
Setting environment variables =?Utf-8?B?Q2FsbCBtZSBCaWxs?= Vista General 2 09-07-2006 03:54 PM
Sundeep Raina(Iopsis): Is variables being handled in Workflow and How to declare New Variables sunny Avalon 0 06-30-2006 01:31 AM
Setting environment variables? Chaz Vista account administration 2 06-23-2006 11:26 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008