-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell_profile.ps1
169 lines (144 loc) · 5.63 KB
/
Microsoft.PowerShell_profile.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function Get-CustomTree {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Path,
[switch]$IncludeFiles = $false,
[string[]]$Exclude = @(),
[switch]$ShowSize
)
$chars = @{
interior = "├─"
last = "└─"
hline = "─"
vline = "│"
space = " "
}
function Get-Size {
param (
[string]$ItemPath,
[switch]$IsDirectory
)
if ($IsDirectory) {
# Write-Host "DEBUG: Calculating size for directory: ${ItemPath}" -ForegroundColor Cyan
$files = Get-ChildItem -Path $ItemPath -Recurse -File -ErrorAction SilentlyContinue
if (-not $files) {
# Write-Host "DEBUG: No files found in directory: ${ItemPath}" -ForegroundColor Red
return 0
}
$dirSize = ($files | Measure-Object -Property Length -Sum).Sum
# Write-Host "DEBUG: Total size for directory ${ItemPath}: $dirSize bytes" -ForegroundColor Cyan
return $dirSize
} else {
# Write-Host "DEBUG: Calculating size for file: ${ItemPath}" -ForegroundColor Cyan
try {
$fileSize = (Get-Item -Path $ItemPath -ErrorAction SilentlyContinue).Length
# Write-Host "DEBUG: File size: $fileSize bytes" -ForegroundColor Cyan
return $fileSize
} catch {
# Write-Host "DEBUG: Failed to get size for file: ${ItemPath}" -ForegroundColor Red
return 0
}
}
}
function Format-Size {
param (
[int64]$Bytes
)
if ($null -eq $Bytes) {
return "0 B"
}
switch ($Bytes) {
{ $_ -ge 1GB } { "{0:N2} GB" -f ($Bytes / 1GB); break }
{ $_ -ge 1MB } { "{0:N2} MB" -f ($Bytes / 1MB); break }
{ $_ -ge 1KB } { "{0:N2} KB" -f ($Bytes / 1KB); break }
default { "$Bytes B"; break }
}
}
function Get-Tree {
param (
[string]$CurrentPath,
[string]$Prefix = "",
[switch]$IsLast,
[switch]$ShowSize,
[switch]$IncludeFiles,
[string[]]$Exclude
)
# Write-Host "DEBUG: Entering Get-Tree for path: $CurrentPath" -ForegroundColor Yellow
if ($ShowSize) {
# Write-Host "DEBUG: The -ShowSize switch is active" -ForegroundColor Green
} else {
# Write-Host "DEBUG: The -ShowSize switch is NOT active" -ForegroundColor Red
}
$items = Get-ChildItem -Path $CurrentPath -Force |
Where-Object {
-not ($Exclude -contains $_.Name)
}
$directories = $items | Where-Object { $_.PSIsContainer }
$files = @()
if ($IncludeFiles) {
$files = $items | Where-Object { -not $_.PSIsContainer }
}
$allItems = @()
if ($directories) {
$allItems += $directories
}
if ($files) {
$allItems += $files
}
for ($i = 0; $i -lt $allItems.Count; $i++) {
$item = $allItems[$i]
$isLastItem = ($i -eq $allItems.Count - 1)
$treeChar = if ($isLastItem) { $chars.last } else { $chars.interior }
[string]$size = ""
if ($ShowSize) {
# Write-Host "DEBUG: Processing item: $($item.FullName)" -ForegroundColor Cyan
$sizeBytes = if ($item.PSIsContainer) {
Get-Size -ItemPath $item.FullName -IsDirectory:$true
} else {
Get-Size -ItemPath $item.FullName -IsDirectory:$false
}
if ($sizeBytes -ne $null) {
$size = Format-Size -Bytes $sizeBytes
} else {
# Write-Host "DEBUG: Size calculation returned null for $($item.FullName)" -ForegroundColor Red
}
# Write-Host "DEBUG: Formatted size for item $($item.FullName): $size" -ForegroundColor Cyan
}
$line = if ($ShowSize) {
"{0,-10} {1}{2}" -f $size, $Prefix, "$treeChar $($item.Name)"
} else {
"$Prefix$treeChar $($item.Name)"
}
# Debug: Display constructed line
# Write-Host "DEBUG: Constructed line: $line" -ForegroundColor Green
# Display line with appropriate colors
if ($item.PSIsContainer) {
Write-Host $line -ForegroundColor Yellow
} else {
Write-Host $line -ForegroundColor Green
}
$nextPrefix = $Prefix
if ($isLastItem) {
$nextPrefix += $chars.space + " "
} else {
$nextPrefix += $chars.vline + " "
}
if ($item.PSIsContainer) {
Get-Tree -CurrentPath $item.FullName `
-Prefix $nextPrefix `
-IsLast:$isLastItem `
-ShowSize:$ShowSize `
-IncludeFiles:$IncludeFiles `
-Exclude $Exclude
}
}
}
if (-not (Test-Path -Path $Path)) {
Write-Error "The path '$Path' does not exist."
return
}
$Path = $Path.TrimEnd('\')
Write-Output "Directory: $Path"
Get-Tree -CurrentPath $Path -Prefix "" -ShowSize:$ShowSize -IncludeFiles:$IncludeFiles -Exclude $Exclude
}