2009-07-28

how to set Windows env var with PowerShell?

How to add a Windows envirenment var permanently with PowerShell?

The PowerShell Cookbook by Lee Holmes says this:

$oldPersonalPath = [Environment]::GetEnvironmentVariable("PATH", "USER")
$oldPersonalPath += ";d:\tools"
[Environment]::SetEnvironmentVariable("Path", $oldPersonalPath, "User")

however, when i do

[Environment]::GetEnvironmentVariable("Path", "User")

nothing is returned. Piping it to get-member returns the error: Get-Member : No object has been specified to get-member.

I can do the following but it's just per session.

$env:path = $env:path + ";C:\Users\xah\PowerShell scripts\"

A quick search in this group doesn't seems to find answer.

Thanks.

Xah
∑ http://xahlee.org/



-----------------------------

Larry__Weiss wrote:
> Read this article and especially note the caveat regarding restarting PowerShell.
>

http://www.microsoft.com/technet/scriptcenter/resources/pstips/dec07/pstip1214.mspx

Thanks.

> > [Environment]::GetEnvironmentVariable("Path", "User")
> > nothing is returned.

apparantly my problem was the wrong variable. There's no “Path” env var of User category on my machine.

I now try:

[Environment]::GetEnvironmentVariable("Path", "system")

but that's a error. Apparantly the name is not “system”. What should i put for the second param? It's not clear to me where i'd find this info. Am still grobing Windows system and docs...

Xah
∑ http://xahlee.org/



-------------


Xah wrote:
> > I now try:
> > [Environment]::GetEnvironmentVariable("Path", "system")
> > but that's a error. Apparantly the name is not “system”. What should i
> > put for the second param? It's not clear to me where i'd find this
> > info. Am still grobing Windows system and docs...

Larry__Weiss wrote:
> Read the error message in its entirety. Doesn't it tell you what the valid
> values are for that second argument?

Doh! Thanks.

> Were you able to get to the text of this article?http://www.microsoft.com/technet/scriptcenter/resources/pstips/dec07/...

Yes. Did read the whole thing. Thanks.

Xah
∑ http://xahlee.org/

No comments:

Post a Comment