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 PowerShell | 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

# re: Program Files Environment Variable in PowerShell
Requesting Gravatar...
Your script Looks fine but it is wrong!

You need to put parenthesis around the function call inside the if or the -eq $true will be treated as Parameter to the function call!
Besides that, your logic is turned around! if is64bit return $true saying Yes, I am a 64 bit machine, your result is program files (x86) what means i m a 32 bit machine.
So, as a conclusion: This code is completely wrong but it is a good hint on how to do it.

Another Option would be to put the result from is64bit into a variable and check that variable.

So the correct code would look like this:
function get-programfilesdir() {
if ((is64bit) -eq $true) {
(Get-Item "Env:ProgramFiles").Value
}
else {
(Get-Item "Env:ProgramFiles(x86)").Value
}
}
Left by Andreas on Jan 03, 2017 7:13 AM

# re: Program Files Environment Variable in PowerShell
Requesting Gravatar...
If I have a script searching the location of my 32-bit application , then it really should NOT make any difference if it is running in 32-bit PowerShell of 64-bit PowerShell. However the result of the is64bit function returns true or false depending on the bitness of the current PowerShell process! So in the given context , which is determining the location of 32-bit application, this is64bit function returns utter nonsens!

I would suggest giving the function a desciptive name:

Function Test-64BitProcess() {
return ([IntPtr]::Size -eq 8)
}

Then to test if there even exists an ProgramFiles(x86) folder, you would use this:
if ([environment]::Is64BitOperatingSystem) {
..
}
It would be overkill to make that a function in itself.
Left by Paul Wiegmans on Sep 13, 2017 8:24 AM

Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net