Geeks With Blogs

News

qrcode

Lance Robinson

Create Your Badge

Lance Robinson is a software engineer in Durham, Chapel Hill, Raleigh, and surrounding areas. More about Lance.

 Subscribe


Lance's TextBox » About Me » My Resume »Twitter

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:
Posted on Tuesday, December 29, 2009 7:27 AM | Back to top


Comments on this post: Program Files Environment Variable in PowerShell

# re: Program Files Environment Variable in PowerShell
Requesting 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.
Left by Puddin Tame on Jan 06, 2010 4:25 PM

# re: Program Files Environment Variable in PowerShell
Requesting 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.
Left by chris on Oct 11, 2011 7:33 AM

Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net | Join free