Last active
September 25, 2018 04:25
-
-
Save mklement0/c0a5d8a0aa44369689800c57e2b747c2 to your computer and use it in GitHub Desktop.
Get-SpecialFolder: PowerShell function that retrieves special folder (known folders), i.e., folders with special meaning to the OS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-SpecialFolder { | |
<# | |
.SYNOPSIS | |
Gets special (known) folders. | |
.DESCRIPTION | |
Gets items representing special folders (directories), i.e., | |
folders whose purpose is predefined by the operating system. | |
In a string context, each such item expands to the full, literal path it | |
represents. | |
If no name is given, all special folders known to the current OS | |
are listed. Use -All to include those that are special on other platforms. | |
.PARAMETER Name | |
The name(s) of specific special folders to get. | |
Note that wildcards are not supported. | |
.PARAMETER All | |
Lists all special folders across all supported platforms, i.e., includes | |
folders that aren't defined for the current OS. | |
.EXAMPLE | |
Get-SpecialFolder UserProfile, Desktop | |
Gets items representing the current OS' user-profile (home) folder | |
and desktop folder. | |
.EXAMPLE | |
Get-SpecialFolder | |
Lists all special folders defined for the current OS. | |
Add -All to see special folders across all supported OSs. | |
.EXAMPLE | |
"The application-data folder is: $(Get-SpecialFolder ApplicationData)" | |
Uses the function in an expandable string, showing how the item returned | |
expands to its full, literal directory path. | |
.NOTES | |
Requires version 5 or higher. | |
This function is convenient, but not fast. | |
#> | |
[CmdletBinding(PositionalBinding=$False, DefaultParameterSetName='List')] | |
param( | |
[Parameter(Mandatory, Position=0, ParameterSetName='Name')] | |
[ValidateSet('AdminTools', 'ApplicationData', 'CDBurning', 'CommonAdminTools', 'CommonApplicationData', 'CommonDesktopDirectory', 'CommonDocuments', 'CommonMusic', 'CommonOemLinks', 'CommonPictures', 'CommonProgramFiles', 'CommonProgramFilesX86', 'CommonPrograms', 'CommonStartMenu', 'CommonStartup', 'CommonTemplates', 'CommonVideos', 'Cookies', 'Desktop', 'DesktopDirectory', 'Favorites', 'Fonts', 'History', 'InternetCache', 'LocalApplicationData', 'LocalizedResources', 'MyComputer', 'MyDocuments', 'MyMusic', 'MyPictures', 'MyVideos', 'NetworkShortcuts', 'Personal', 'PrinterShortcuts', 'ProgramFiles', 'ProgramFilesX86', 'Programs', 'Recent', 'Resources', 'SendTo', 'StartMenu', 'Startup', 'System', 'SystemX86', 'Templates', 'UserProfile', 'Windows')] | |
[string[]] $Name | |
, | |
[Parameter(ParameterSetName='List')] | |
[switch] $All | |
) | |
[Flags()] | |
enum GsfSupportedOs { | |
All = 0x7 # !! Sum of all the flags below - BE SURE To UPDATE THIS IF NEW FLAGS ARE ADDED | |
Windows = 0x1 | |
Linux = 0x2 | |
macOS = 0x4 | |
} | |
class GsfSpecialFolder { | |
[string] $Name | |
[string] $Path | |
[GsfSupportedOs] $SupportedOs | |
[string] ToString() { return $this.Path } | |
} | |
# Note: The SupportedOs flags were determined by trial and error on each platform, as of .NET Core 2.1 | |
# To get all supported names, use: | |
# [enum]::GetNames([System.Environment+SpecialFolder]) | Sort-Object | |
$specialFolders = [ordered] @{ | |
'AdminTools' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'ApplicationData' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'CDBurning' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonAdminTools' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonApplicationData' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'CommonDesktopDirectory' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonDocuments' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonMusic' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonOemLinks' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonPictures' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonProgramFiles' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonProgramFilesX86' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonPrograms' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonStartMenu' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonStartup' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonTemplates' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'CommonVideos' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'Cookies' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'Desktop' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'DesktopDirectory' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'Favorites' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS' } | |
'Fonts' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS' } | |
'History' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'InternetCache' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS' } | |
'LocalApplicationData' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'LocalizedResources' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'MyComputer' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'MyDocuments' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'MyMusic' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'MyPictures' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'MyVideos' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, Linux' } | |
'NetworkShortcuts' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'PrinterShortcuts' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'ProgramFiles' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS' } | |
'ProgramFilesX86' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'Programs' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'Recent' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'Resources' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'SendTo' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'StartMenu' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'Startup' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'System' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS' } | |
'SystemX86' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
'Templates' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, Linux' } | |
'UserProfile' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows, macOS, Linux' } | |
'Windows' = [GsfSpecialFolder] @{ Name = ''; Path = $null; SupportedOs = [GsfSupportedOs] 'Windows' } | |
} | |
# Determine the filter to apply to the list of all special folders. | |
if ($PSCmdlet.ParameterSetName -eq 'List') { | |
$thisOs = if ($IsMacOS) { [GsfSupportedOs]::macOS } elseif ($IsLinux) { [GsfSupportedOs]::Linux } else { [GsfSupportedOs]::Windows } | |
$sbFilter = if ($All) { { $True } } else { { $_.Value.SupportedOs -band $thisOS } } | |
} else { | |
$sbFilter = { $_.Key -in $Name } | |
} | |
# Get the filtered list and flesh out the output objects. | |
$specialFolders.GetEnumerator() | Where-Object $sbFilter | ForEach-Object { | |
$_.Value.Name = $_.Key | |
$_.Value.Path = [Environment]::GetFolderPath($_.Key) | |
$_.Value | |
} | |
} | |
# If this script is invoked directly - as opposed to being dot-sourced in order | |
# to define the embedded function for later use - invoke the embedded function, | |
# relaying any arguments passed. | |
if (-not ($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq '')) { | |
Get-SpecialFolder @Args | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment