-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Set-OutDefaultOverride.ps1
72 lines (67 loc) · 2.55 KB
/
Set-OutDefaultOverride.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
New-CommandWrapper Out-Default -Process {
$nocase = [Text.RegularExpressions.RegexOptions]::IgnoreCase
$compressed = New-Object Text.RegularExpressions.Regex('\.(zip|tar|gz|iso)$', $nocase)
$executable = New-Object Text.RegularExpressions.Regex('\.(exe|bat|cmd|msi|ps1|psm1)$', $nocase)
$unix = New-Object Text.RegularExpressions.Regex('\.(sh)$', $nocase)
if (($_ -is [IO.DirectoryInfo]) -or ($_ -is [IO.FileInfo]))
{
# this function is run in a new scope for each Get-ChildItem call and that scope
# is reused until that invokation is complete which means we can take advantage
# of that scope to "cache" our own information like $junctions
if (-not $notfirst)
{
$parent = [IO.Path]::GetDirectoryName($_.FullName)
Write-Host "`n Directory: $parent`n"
Write-Host 'Mode Last Write Time Length Name'
Write-Host '---- --------------- ------ ----'
$notfirst = $true
$junctions = cmd /c "dir /al $parent" | ? { $_ -match '<JUNCTION>' }
}
$name = $_.Name
if ($_ -is [IO.DirectoryInfo])
{
Write-Host ('{0} {1,11} {2,8}' -f $_.mode, $_.LastWriteTime.ToString('d'), $_.LastWriteTime.ToString('t')) -NoNewline
if ($_.LinkType)
{
Write-Host ('{0,12} ' -f 'symlink') -ForegroundColor DarkGray -NoNewline
Write-Host $name -ForegroundColor Blue -NoNewline
$target = ($_ | select -expand Target).Replace('UNC\', '\\')
Write-Host " > $target" -ForegroundColor DarkBlue
}
elseif ($_.Attributes -match 'ReparsePoint')
{
if (($junctions | ? { $_ -match "$name \[" }) -match '\[([^\]]+)\]$')
{
Write-Host ('{0,12} ' -f 'junction') -ForegroundColor DarkGray -NoNewline
Write-Host $name -ForegroundColor Blue -NoNewline
Write-Host " > $($matches[1])" -ForegroundColor DarkBlue
}
else
{
Write-Host ('{0,12} ' -f '') -NoNewline
Write-Host $name -ForegroundColor Blue
}
}
else
{
Write-Host ('{0,12} ' -f '') -NoNewline
Write-Host $name -ForegroundColor Blue
}
}
else
{
if ($compressed.IsMatch($name)) { $color = 'Magenta' }
elseif ($executable.IsMatch($name)) { $color = 'DarkGreen' }
elseif ($unix.IsMatch($name)) { $color = 'Cyan' }
else { $color = 'Gray' }
# ensure size fits within the 11 char column
$size = $_.Length
if ($size -gt 90GB) { $size = ('{0} GB' -f ($size / 1GB).ToString('0.###')) }
Write-Host ('{0} {1,11} {2,8} {3,11} ' -f $_.mode, $_.LastWriteTime.ToString('d'), $_.LastWriteTime.ToString('t'), $size) -NoNewline
Write-Host $name -ForegroundColor $color
}
$_ = $null
}
} -end {
Write-Host
}