Powershell tidbit: hacky way to find the 32-bit Program Files directory

In the process of tidying up another function for posting, I found that there is no clean way to get a Powershell variable from the system that:

  1. Points to “C:\program files (x86)” regardless whether you’re in a 32-bit or 64-bit process.
  2. Exists on legacy 32-bit operating systems.

Sounds like a common thing you’d need: to find the location where 32-bit programs are installed regardless of any other factors.  Powershell’s $env:ProgramFiles(x86) variable comes close, but even if it were present on 32-bit OSes -- honestly not sure about that -- the parentheses in the variable name present a nasty parse problem.  No amount of quoting or backtick`ing or $()’ing got me the result I was looking for.  If you have more patience than me, have at it.

And oh yeah, did I mention I was shopping for a better null coalescing operator than the one that comes with PSCX?  Note to PSCX folks: I hate wrapping stuff in scriptblocks when there’s no real need for them.  Thanks, Richard’s pinky finger.

So anyway, I resorted to putting this abomination in my $profile:

001
002
003
004
function ?? {
    (@($args | ?{$_}) + $null)[0]
}
set-variable programFilesX86 (?? (cat 'Env:\ProgramFiles(x86)' -ea SilentlyContinue) $Env:ProgramFiles) -scope global

Yes, it’s ugly and nonstandard (in the sense that I can’t rely on this being present in other people’s script environments)…suggestions welcome...

One Response to “Powershell tidbit: hacky way to find the 32-bit Program Files directory”

  1. BUGBUG: poor title » Blog Archive » Invoke-UltraEdit.ps1: integrate your favorite text editor with the Powershell pipeline Says:

    [...] same thing we do every night, Pinky… « Easily manage files in Powershell ISE CTP3 Powershell tidbit: hacky way to find the 32-bit Program Files directory [...]

Leave a Reply