Wednesday, 5 August 2009

Get-SpecialFolders.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script lists the special folders enumerated in System.Environment.SpecialFolder 
  4. .DESCRIPTION 
  5.     This script first enumerates the SpecialFolder Enum. for each member, the script 
  6.     then looks up, and displays, the value of that folder. 
  7. .NOTES 
  8.     File Name  : Get-SpecialFolders.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Environment\Get-SpecialFolders.PS1' 
  18.     Folder Name            Path 
  19.     -----------            ----------------------------------------------- 
  20.     Desktop                C:\Users\tfl\Desktop 
  21.     Programs               C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs 
  22.     Personal               C:\Users\tfl\Documents 
  23.     Personal               C:\Users\tfl\Documents 
  24.     Favorites              C:\Users\tfl\NetHood\Favorites 
  25.     Startup                C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 
  26.     Recent                 C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Recent 
  27.     SendTo                 C:\Users\tfl\AppData\Roaming\Microsoft\Windows\SendTo 
  28.     StartMenu              C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu 
  29.     MyMusic                C:\Users\tfl\Music 
  30.     DesktopDirectory       C:\Users\tfl\Desktop 
  31.     MyComputer 
  32.     Templates              C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Templates 
  33.     ApplicationData        C:\Users\tfl\AppData\Roaming 
  34.     LocalApplicationData   C:\Users\tfl\AppData\Local 
  35.     InternetCache          C:\Users\tfl\AppData\Local\Microsoft\Windows\Temporary Internet Files 
  36.     Cookies                C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Cookies 
  37.     History                C:\Users\tfl\AppData\Local\Microsoft\Windows\History 
  38.     CommonApplicationData  C:\ProgramData 
  39.     System                 C:\Windows\system32 
  40.     ProgramFiles           C:\Program Files (x86) 
  41.     MyPictures             C:\Users\tfl\Pictures 
  42.     CommonProgramFiles     C:\Program Files (x86)\Common Files 
  43. #> 
  44.  
  45. ## 
  46. # Start of Script 
  47. ## 
  48.  
  49. # Get the list of special folders 
  50. $folders = [system.Enum]::GetValues([System.Environment+SpecialFolder])  
  51.  
  52. # Display these folders 
  53. "Folder Name            Path" 
  54. "-----------            -----------------------------------------------" 
  55. foreach ($folder in $folders) { 
  56. "{0,-22} {1,-15}"  -f $folder,[System.Environment]::GetFolderPath($folder
  57. #End of Script 

5 comments:

slipsec said...

I'm curious why you choose to return strings not objects (say [System.IO.DirectoryInfo]). I do love the enum trick though, they are my newest friends.

Thomas Lee said...

Thanks for the comment.

I wrote the script simply to enumerate each Special Folder and where on my system it pointed to. I used the ENUM to work out what the set of special folder names were, then I called GetFolderPath to get the path for that folder, which I then display. If I'd wanted to do more things, then using the DirectoryInfo object might have been useful.

David Moisan said...

I borrowed code from this script. I know how the code works to get special folders. What I don't know is the syntax:
$folders = [system.Enum]::GetValues([System.Environment+SpecialFolder])

Why System.Environment+SpecialFolder? What is different (other than that System.Environment.SpecialFolder does not work in this instance)?

I hate using code without understanding the syntax, though I know what it does, it is frustrating; I have never seen the syntax mentioned anywhere else.

Thomas Lee said...

David,

I'm not sure why this particular bit of syntax (i.e. using the "+") works and why normal dotting doesn't. I spent hours trying to figure this out and came up with little to explain it, other than this works. I'd love to know too, but I just chalk it up to 'you just have to know'!

Thanks for your comment.

Tom said...

The reason for the plus sign is due to public child classes. SpecialFolder is a public class nested within the System.Environment class. When referencing public child classes you have to use a + sign. We posted an article about this a while back on the PowerShell blog: http://blogs.msdn.com/b/powershell/archive/2009/08/27/plus-in-net-class-names.aspx

-- Tom