24/7/365 Support

Find the Location of Common System Paths in PowerShell

Problem

You want to know the location of common system paths and special folders, such as My Documents and Program Files.

Solution

To determine the location of common system paths and special folders, use the [Environment]::GetFolderPath() method: PS >[Environment]::GetFolderPath("System") C:\WINDOWS\system32

For paths not supported by this method (such as All Users Start Menu), use the WScript.Shell COM object: $shell = NewObject Com WScript.Shell $allStartMenu = $shell.SpecialFolders.Item("AllUsersStartMenu")

Discussion

The [Environment]::GetFolderPath() method lets you access the many common locations used in Windows. To use it, provide the short name for the location (such as System or Personal). Since you probably don’t have all these short names memorized, one way to see all these values is to use the [Enum]::GetValues() method, as shown in Example 142.

Example 142. Folders supported by the [Environment]::GetFolderPath() method

PS >[Enum]::GetValues([Environment+SpecialFolder]) Desktop Programs Personal Favorites Startup Recent SendTo StartMenu MyMusic DesktopDirectory MyComputer Templates ApplicationData LocalApplicationData InternetCache Cookies History CommonApplicationData

Example 142. Folders supported by the [Environment]::GetFolderPath() method (continued)

System ProgramFiles MyPictures CommonProgramFiles

Since this is such a common task for all enumerated constants, though, PowerShell actually provides the possible values in the error message if it is unable to convert your input:

PS >[Environment]::GetFolderPath("aouaoue") Cannot convert argument "0", with value: "aouaoue", for "GetFolderPath" to type "System.Environment+SpecialFolder": "Cannot convert value "aouaoue" to type "System.Environment+SpecialFolder" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Desktop, Programs, Personal, MyDocuments, Favorites, Startup, Recent, SendTo, StartMenu, MyMusic, DesktopDirectory, MyComputer, Templates, ApplicationData, LocalApplicationData, InternetCache, Cookies, History, CommonApplicationData, System, ProgramFiles, MyPictures, CommonProgramFiles"." At line:1 char:29

+ [Environment]::GetFolderPath( <<<< "aouaoue")

Although this method provides access to the mostused common system paths, it does not provide access to all of them. For the paths that the [Environment]:: GetFolderPath() method does not support, use the WScript.Shell COM object. The WScript.Shell COM object supports the following paths: AllUsersDesktop, AllUsersStartMenu, AllUsersPrograms, AllUsersStartup, Desktop, Favorites, Fonts, MyDocuments, NetHood, PrintHood, Programs, Recent, SendTo, StartMenu, Startup, and Templates.

It would be nice if you could use either the [Environment]::GetFolderPath() method or the WScript.Shell COM object, but each of them supports a significant number of paths that the other does not, as Example 143 illustrates.

Example 143. Differences between folders supported by [Environment]::GetFolderPath() and the Wscript.Shell COM object

PS >$shell = NewObject Com WScript.Shell PS >$shellPaths = $shell.SpecialFolders | SortObject PS > PS >$netFolders = [Enum]::GetValues([Environment+SpecialFolder]) PS >$netPaths = $netFolders | >> ForeachObject { [Environment]::GetFolderPath($_) } | SortObject >> PS >## See the shellonly paths PS >CompareObject $shellPaths $netPaths | >> WhereObject { $_.SideIndicator eq "<=" } >>

Example 143. Differences between folders supported by [Environment]::GetFolderPath() and the Wscript.Shell COM object (continued)

InputObject SideIndicator

C:\Documents and Settings\All Users\Desktop <= C:\Documents and Settings\All Users\Start Menu <= C:\Documents and Settings\All Users\Start Menu\Programs <= C:\Documents and Settings\All Users\Start Menu\Programs\... <= C:\Documents and Settings\Lee\NetHood <= C:\Documents and Settings\Lee\PrintHood <= C:\Windows\Fonts <=

PS >## See the .NETonly paths PS >CompareObject $shellPaths $netPaths | >> WhereObject { $_.SideIndicator eq "=>" } >>

InputObject SideIndicator

=> C:\Documents and Settings\All Users\Application Data => C:\Documents and Settings\Lee\Cookies => C:\Documents and Settings\Lee\Local Settings\Application... => C:\Documents and Settings\Lee\Local Settings\History => C:\Documents and Settings\Lee\Local Settings\Temporary I... => C:\Program Files => C:\Program Files\Common Files => C:\WINDOWS\system32 => d:\lee => D:\Lee\My Music => D:\Lee\My Pictures =>

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say