- <#
- .SYNOPSIS
- This script lists the special folders enumerated in System.Environment.SpecialFolder
- .DESCRIPTION
- This script first enumerates the SpecialFolder Enum. for each member, the script
- then looks up, and displays, the value of that folder.
- .NOTES
- File Name : Get-SpecialFolders.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Environment\Get-SpecialFolders.PS1'
- Folder Name Path
- ----------- -----------------------------------------------
- Desktop C:\Users\tfl\Desktop
- Programs C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
- Personal C:\Users\tfl\Documents
- Personal C:\Users\tfl\Documents
- Favorites C:\Users\tfl\NetHood\Favorites
- Startup C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
- Recent C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Recent
- SendTo C:\Users\tfl\AppData\Roaming\Microsoft\Windows\SendTo
- StartMenu C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu
- MyMusic C:\Users\tfl\Music
- DesktopDirectory C:\Users\tfl\Desktop
- MyComputer
- Templates C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Templates
- ApplicationData C:\Users\tfl\AppData\Roaming
- LocalApplicationData C:\Users\tfl\AppData\Local
- InternetCache C:\Users\tfl\AppData\Local\Microsoft\Windows\Temporary Internet Files
- Cookies C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Cookies
- History C:\Users\tfl\AppData\Local\Microsoft\Windows\History
- CommonApplicationData C:\ProgramData
- System C:\Windows\system32
- ProgramFiles C:\Program Files (x86)
- MyPictures C:\Users\tfl\Pictures
- CommonProgramFiles C:\Program Files (x86)\Common Files
- #>
- ##
- # 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
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Wednesday, 5 August 2009
Get-SpecialFolders.ps1
Subscribe to:
Post Comments (Atom)
5 comments:
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.
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.
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.
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.
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
Post a Comment