Accessing User MyDocuments Folder Path via Powershell

Sample Code:

PS C:> $MyDocs = [System.Environment]::GetFolderPath("MyDocuments")
PS C:> Write-host $MyDocs
C:UsersTestUser1Documents
PS C:>

 

Listing all the SpecialFolders available via Environment Variables.

Code:

cls
### Start of Script

### Get the list of special folders
$folders = [system.Enum]::GetValues([System.Environment+SpecialFolder])

# Display these folders
"Folder Name            Path"
"———–            ———————————————–"

foreach ($folder in $folders) {
    "{0,-22} {1,-15}"  -f $folder,[System.Environment]::GetFolderPath($folder)
    }

#End of Script

 

Output:

Folder Name            Path
———–            ———————————————–
Desktop                C:UsersTestuser1Desktop
Programs               C:UsersTestuser1AppDataRoamingMicrosoftWindowsStart MenuPrograms
Personal               C:UsersTestuser1Documents
Personal               C:UsersTestuser1Documents
Favorites              C:UsersTestuser1Favorites
Startup                C:UsersTestuser1AppDataRoamingMicrosoftWindowsStart MenuProgramsStartup
Recent                 C:UsersTestuser1AppDataRoamingMicrosoftWindowsRecent
SendTo                 C:UsersTestuser1AppDataRoamingMicrosoftWindowsSendTo
StartMenu              C:UsersTestuser1AppDataRoamingMicrosoftWindowsStart Menu
MyMusic                C:UsersTestuser1Music
DesktopDirectory       C:UsersTestuser1Desktop
MyComputer                           
Templates              C:UsersTestuser1AppDataRoamingMicrosoftWindowsTemplates
ApplicationData        C:UsersTestuser1AppDataRoaming
LocalApplicationData   C:UsersTestuser1AppDataLocal
InternetCache          C:UsersTestuser1AppDataLocalMicrosoftWindowsTemporary Internet Files
Cookies                C:UsersTestuser1AppDataRoamingMicrosoftWindowsCookies
History                C:UsersTestuser1AppDataLocalMicrosoftWindowsHistory
CommonApplicationData  C:ProgramData
System                 C:Windowssystem32
ProgramFiles           C:Program Files
MyPictures             C:UsersTestuser1Pictures
CommonProgramFiles     C:Program FilesCommon Files

 

Reference:

  1. Environment.SpecialFolder Enumeration
  2. c# – Expand environment variable for My Documents – Stack Overflow

Leave a Reply

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