Skip to content

Instantly share code, notes, and snippets.

@TexRx
Created March 30, 2012 02:22
Show Gist options
  • Save TexRx/2245880 to your computer and use it in GitHub Desktop.
Save TexRx/2245880 to your computer and use it in GitHub Desktop.
PS: Get-SpecialPath
# STEP 0: Declare input parameter
Param([switch]$AsDrive)
# STEP 1: Define hash for paths
$csidl = @{
"Personal" = 0x0005;
"LocalAppData" = 0x001c;
"InternetCache" = 0x0020;
"Cookies" = 0x0021;
"History" = 0x0022;
"Windows" = 0x0024;
"System" = 0x0025;
"ProgramFiles" = 0x0026;
"ProgramFilesx86" = 0x002a;
"Profile" = 0x0028;
"AdminTools" = 0x0030;
}
# STEP 2: Set up Shell.Application COM object
$sa = New-Object -ComObject Shell.Application
# STEP 3: Set up hask for paths, collect with "names"
$sp = @{} # the special paths collection
foreach($key in $csidl.keys)
{
$sp[$key] = $sa.NameSpace($csidl[$key]).Self.Path;
}
# STEP 4: Map global drives using names from the hash
if($AsDrive) # Param declared at the beginning
{
$keys = $sp.keys;
$keys | %{
$p = $sp[$_];
if($p.Length -gt 0)
{
$n = $csidl[$_] # numeric to help ID the drive...
New-PSDrive -Name: $_ -PSProvider:FileSystem -Root:$p `
-Scope Global -Description:"SpecialFolder$n"
}
}
} else {
return $sp;
}
# http://www.vistax64.com/powershell/19524-need-quick-way-get-my-documents-folder.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment