-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Get-SpecialFolder.ps1
96 lines (83 loc) · 2.38 KB
/
Get-SpecialFolder.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<#
.SYNOPSIS
Return the translation of a SpecialFolder by name or show all SpecialFolders.
.DESCRIPTION
This command runs in two modes based on the switches, one and all. By default, it returns
the value of the named SpecialFolder by assuming the -one switch. If the -all switch is
specified then all SpecialFolders are shown and the folder parameter is used to highlight
matching results.
.PARAMETER folder
The name of the SpecialFolder enumeration to return or a substring of the folder names
to highlight
.PARAMETER all
Show all SpecialFolders.
.PARAMETER one
The default. Return the value of the named SpecialFolder.
#>
param (
[Parameter(Position=0)] [string] $folder,
[switch] $all)
if ($all)
{
$format = '{0,-30} {1}'
Write-Host ($format -f 'Name', 'Value')
Write-Host ($format -f '----', '-----')
$folders = @{}
[Enum]::GetValues('System.Environment+SpecialFolder') | % `
{
if (!($folders.ContainsKey($_.ToString())))
{
$folders.Add($_.ToString(), [Environment]::GetFolderPath($_))
}
}
$folders.GetEnumerator() | sort name | % `
{
$name = $_.Name.ToString()
if ($name.Length -gt 30) { $name = $name.Substring(0,27) + '...' }
$value = $_.Value.ToString()
$max = $host.UI.RawUI.WindowSize.Width - 32
if ($value.Length -gt $max) { $value = $value.Substring(0, $max - 3) + '...' }
# when -all then $folder is a match string
if ($folder -and ($_.Name -match $folder))
{
Write-Host ($format -f $name, $value) -ForegroundColor Green
}
elseif ([String]::IsNullOrEmpty($folder))
{
if ($_.Name -eq 'CommonApplicationData')
{
Write-Host ($format -f $name, $value) -ForegroundColor Blue
}
elseif ($_.Name -match 'ApplicationData')
{
Write-Host ($format -f $name, $value) -ForegroundColor Magenta
}
elseif ($value -match "$env:USERNAME(?:\\\w+){0,1}$")
{
Write-Host ($format -f $name, $value) -ForegroundColor DarkGreen
}
else
{
Write-Host ($format -f $name, $value)
}
}
else
{
Write-Host ($format -f $name, $value)
}
}
}
else
{
if ([String]::IsNullOrEmpty($folder))
{
Write-Host "Specify a SpecialFolder name or -all" -ForegroundColor Yellow
exit 1
}
if (-not [bool]($folder -as [Environment+SpecialFolder] -is [Environment+SpecialFolder]))
{
Write-Host "$folder is not a valid SpecialFolder name" -ForegroundColor Red
exit 1
}
[Environment]::GetFolderPath($folder -as [Environment+SpecialFolder])
}