TIMTOWTDI: There is more than one way to do it with PowerShell as well.

TIMTOWTDI has been the catch cry of Perl hackers for as long as I’ve been familiar with the language and the interpreter. With PowerShell being an interactive shell first and foremost it is obvious that it would offer the same services.

Today I was reading a PowerShell mailing list and one of the posters asked how to permanently modify the PATH environment variable. I thought for a moment then jumped into action. My feeble attempt is attached.

I then looked back at the other responses which had come in, and there was this little gem attached:

[Environment]::SetEnvironmentVariable(“Path”, “$Env:Path;C:\Directory”, “Machine”)

I should have known this since I am a .NET developer, but I automatically jumped into Cmdlet/Provider space. There is definitely more than one way to do it with PowerShell J

4 thoughts on “TIMTOWTDI: There is more than one way to do it with PowerShell as well.

  1. /\/\o\/\/

    Sure there are more way’s :

    # COM

    $env = (new-object -com “WScript.Shell”).Environment(“SYSTEM”)
    $env.item(“Path”) = “C:\Directory”

    # WMI

    $env = get-WMIObject win32_environment -filter “Name = ‘Path’
    $env.VariableValue = $env.VariableValue + “;c:\test”

    Greetings /\/\o\/\/

  2. /\/\o\/\/

    Correction on WMI example, and Registry example

    # WMI

    $env = get-WMIObject win32_environment -filter ‘Name = “Path”‘
    $env.VariableValue = $env.VariableValue + “;c:\test”
    $env.put()

    # Registry

    $p = gp -Path “HKLM:\System\CurrentControlSet\Control\Session manager\Environment\” “Path”
    sp -Path “HKLM:\System\CurrentControlSet\Control\Session manager\Environment\” “Path” ($p.path + “;c:\Directory”)

  3. Mitch Denny

    Hi MoW,

    The second example (registry) is pretty close to how I did it in my original script. Having to call out to WMI or WSH always seemed less appropriate to me for some reason.

    Hi jackymko,

    I didn’t know about the setx.exe utility.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s