Blog on software engineering
How to determine the program files folder with PowerShell, by using enums and nested types

How to determine the program files folder with PowerShell, by using enums and nested types

If you want to use the systems “%PROGRAMFILES%” folder in PowerShell, use:

 

$rootInstallationFolder = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::ProgramFiles)
$rootInstallationFolder

Result

C:\Program Files (x86)

Note

Because SpecialFolders is a nested type in System.Environment, you must use the “+” sign and not a “.”, else you get the error:

Unable to find type [System.Environment.SpecialFolder]: make sure that the assembly containing this type is loaded.

One comment

  1. Nope got back

    PS C:\Windows\system32> $rootfolder=[system.environment]::getfolderpath([System.Environment+specialfolder]::ProgramFiles)
    $rootfolder
    C:\Program Files

    vande

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.