Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Changing the PATH variable in the registry with Powershell

Reply
 
Old 04-22-2008   #1 (permalink)
meissnersd


 
 

Changing the PATH variable in the registry with Powershell

It would be really nice to be able to manipulate the Path variable.

Adding to the end of $env:Path will change the Path variable for the session
of shell in powershell. But then your changes go poof when you close the
session.

If you want to change the path permanently...you need to edit the registry.
This is pretty easy to do.

$original = (Get-ItemProperty
"HKLM:SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path
).Path

Set-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\Session
Manager\Environment" Path -value ( $original + ";" + $dirname )

The issue is that registry settings are only read at login.
Starting a new powershell or cmd shell does not pick up the new settings
until I reboot.
When I google it seems to indicate I need to "broadcast" the new registry
settings. Something to do with Windows SendMessage

How do I do that from powershell? Do I need to do P/Invoke? Is there an
easy way from within powershell?

Karl




My System SpecsSystem Spec
Old 04-22-2008   #2 (permalink)
Jon


 
 

Re: Changing the PATH variable in the registry with Powershell

A simpler method would be to add a line like this to your profile eg

$envath+=";C:\pathextra"

--
Jon


"meissnersd" <meissnersd@xxxxxx> wrote in message
news:FA52D495-262A-434C-B0E2-A3CE151D8078@xxxxxx
Quote:

> It would be really nice to be able to manipulate the Path variable.
>
> Adding to the end of $env:Path will change the Path variable for the
> session
> of shell in powershell. But then your changes go poof when you close the
> session.
>
> If you want to change the path permanently...you need to edit the
> registry.
> This is pretty easy to do.
>
> $original = (Get-ItemProperty
> "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path
> ).Path
>
> Set-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\Session
> Manager\Environment" Path -value ( $original + ";" + $dirname )
>
> The issue is that registry settings are only read at login.
> Starting a new powershell or cmd shell does not pick up the new settings
> until I reboot.
> When I google it seems to indicate I need to "broadcast" the new registry
> settings. Something to do with Windows SendMessage
>
> How do I do that from powershell? Do I need to do P/Invoke? Is there an
> easy way from within powershell?
>
> Karl
>
>
>
My System SpecsSystem Spec
Old 04-22-2008   #3 (permalink)
meissnersd


 
 

Re: Changing the PATH variable in the registry with Powershell

well that would work for things within powershell.
Tut I have tools and utilities which need the "real" path set which is why I
need to set it in the registry.


"Jon" wrote:
Quote:

> A simpler method would be to add a line like this to your profile eg
>
> $envath+=";C:\pathextra"
>
> --
> Jon
>
>
> "meissnersd" <meissnersd@xxxxxx> wrote in message
> news:FA52D495-262A-434C-B0E2-A3CE151D8078@xxxxxx
Quote:

> > It would be really nice to be able to manipulate the Path variable.
> >
> > Adding to the end of $env:Path will change the Path variable for the
> > session
> > of shell in powershell. But then your changes go poof when you close the
> > session.
> >
> > If you want to change the path permanently...you need to edit the
> > registry.
> > This is pretty easy to do.
> >
> > $original = (Get-ItemProperty
> > "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path
> > ).Path
> >
> > Set-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\Session
> > Manager\Environment" Path -value ( $original + ";" + $dirname )
> >
> > The issue is that registry settings are only read at login.
> > Starting a new powershell or cmd shell does not pick up the new settings
> > until I reboot.
> > When I google it seems to indicate I need to "broadcast" the new registry
> > settings. Something to do with Windows SendMessage
> >
> > How do I do that from powershell? Do I need to do P/Invoke? Is there an
> > easy way from within powershell?
> >
> > Karl
> >
> >
> >
>
>
My System SpecsSystem Spec
Old 04-22-2008   #4 (permalink)
Jon


 
 

Re: Changing the PATH variable in the registry with Powershell

"meissnersd" <meissnersd@xxxxxx> wrote in message
news:7194743F-93EC-4505-9040-2D8DBE1BEAA7@xxxxxx
Quote:

> well that would work for things within powershell.
> Tut I have tools and utilities which need the "real" path set which is why
> I
> need to set it in the registry.
>



You could use the static '[environment]::SetEnvironmentVariable' method,
specifying an 'EnvironmentVariableTarget' as the third parameter eg an
example setting an environment variable 'apples'


[environment]::SetEnvironmentVariable("apples","three","User")
[environment]::SetEnvironmentVariable("apples","three","Process")

$env:apples

OR

[environment]::SetEnvironmentVariable("apples","three","Machine")
[environment]::SetEnvironmentVariable("apples","three","Process")


Similarly for the 'path' environment variable, but use with caution.

cf

EnvironmentVariableTarget Enumeration
http://msdn2.microsoft.com/en-us/lib...et(VS.80).aspx

--
Jon



My System SpecsSystem Spec
Old 04-25-2008   #5 (permalink)
Keith Hill [MVP]


 
 

Re: Changing the PATH variable in the registry with Powershell

"meissnersd" <meissnersd@xxxxxx> wrote in message
news:7194743F-93EC-4505-9040-2D8DBE1BEAA7@xxxxxx
Quote:

> well that would work for things within powershell.
> Tut I have tools and utilities which need the "real" path set which is why
> I
> need to set it in the registry.
>
>
> "Jon" wrote:
>
Quote:

>> A simpler method would be to add a line like this to your profile eg
>>
>> $envath+=";C:\pathextra"
>>
You probably already know this but if you do what Jon suggests above, all
tools and utilities invoked from that PowerShell session will get the
updated path. That is, they will inherit PowerShell's process env block.

--
Keith


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
spaces and variable in path VB Script
Path Environment Variable - Vista 64-bit Vista General
RE: Execute from command line with environment variable in path PowerShell
Env. PATH variable not working sometimes... Vista General
How can I ensure that a variable is a built-in powershell variable? PowerShell


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46