Program Files Environment Variable in PowerShell

Today I saw this on Twitter from Julie Blender (@#juneb_get_help): 

Need to reference the "ProgramFiles(x86)" environment variable in #PowerShell? Use ${env:ProgramFiles(x86)}.

I use this in a lot of my scripts, but I want a single variable that contains the right value whether I'm in a 64 bit shell or not.  Here's my solution:

First, I have a function in my testing profile called is64bit, that looks like this:

function is64bit() {
  return ([IntPtr]::Size -eq 8)
}

Next, I have a get-programfilesdir function that I call that checks whether or not I'm in a 64 bit shell or not and returns the appropriate program files environment variable (ie, if I'm running on a 64 bit machine it will return "C:\Program Files (x86)", but if I'm running on a 32 bit machine it will return "C:\Program Files".  If I'm running in a 32 or 64 bit shell on a 64 bit machine, it will always return "C:\Program Files (x86)".

function get-programfilesdir() {
  if (is64bit -eq $true) {
    (Get-Item "Env:ProgramFiles(x86)").Value
  }
  else {
    (Get-Item "Env:ProgramFiles").Value
  }
}

 

Technorati Tags:

Print | posted on Tuesday, December 29, 2009 7:27 AM

Feedback

# re: Program Files Environment Variable in PowerShell

Left by Puddin Tame at 1/6/2010 4:25 PM
Gravatar I'm a huge fan of your blog. However, I don't understand anything you write because I know little to nothing about computers other than how to turn it on and check my email. I would like to hear more about how wonderful your girlfriend is.

# re: Program Files Environment Variable in PowerShell

Left by chris at 10/11/2011 7:33 AM
Gravatar I agree with Puddin Tame that the articles in your blog are beyond the average computer user. It would be good to see some articles on how to sort email and how to rename a folder in Windows.

Your comment:





 

Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski