-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongPathFinder.ps1
166 lines (155 loc) · 5.7 KB
/
LongPathFinder.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
#Requires -RunAsAdministrator
#Find paths longer than supported amount
#Windows max w/ long path: 32,767
#Windows max: 260
#OneDrive max: 250
$MaxLength = 250
#Reg key for long path setting
$LongPathsKey = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem')
#Ask to change reg key value
$AskToSetLongPaths = $false
#Only check for long path settings
$OnlyCheckSettings = $true
#User folder to check, will be overriden by $AllUsers if set to $true
[System.IO.DirectoryInfo]$Root = $env:USERPROFILE
#Recurse all user folders, will override $Root value if set to $true
$AllUsers = $false
#-----------
$Error.Clear()
Clear-Host
Write-Host '
================
Long Path Finder
================
'
#Variable checks
if ($MaxLength -notin 1..32767) {
Write-Host -ForegroundColor Red 'Enter a valid number
'
exit 2
}
if (($AskToSetLongPaths -ne $true) -and ($AskToSetLongPaths -ne $false)) {
Write-Host -ForegroundColor Red '$AskToSetLongPaths needs to be $true or $false
'
exit 2
}
if (($OnlyCheckSettings -ne $true) -and ($OnlyCheckSettings -ne $false)) {
Write-Host -ForegroundColor Red '$OnlyCheckSettings needs to be $true or $false
'
exit 2
}
if ($OnlyCheckSettings -eq $false) {
if (($AllUsers -ne $true) -and ($AllUsers -ne $false)) {
Write-Host -ForegroundColor Red '$AllUsers needs to be $true or $false
'
exit 2
} elseif ($AllUsers -eq $true) {
#Check if $Root folder exists
[System.IO.DirectoryInfo]$Root = $env:SystemDrive + '\' + 'Users'
if (($Root.Exists -eq $false) -or ($null -eq $Root.Exists)) {
Write-Host -ForegroundColor Red 'Could not find $Root folder
'
exit 2
}
#If $AllUsers = $true get all users folders info
$UserFolders = Get-ChildItem $Root
Write-Host "Users: $($UserFolders.Count)
"
$UserCount = 1
$AllItems = foreach ($User in $UserFolders) {
Write-Host "Checking user ($UserCount) $($User.Name)"
$UserCount = $UserCount + 1
Get-ChildItem -Path $User.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host ''
} elseif ($AllUsers -eq $false) {
if (($Root.Exists -eq $false) -or ($null -eq $Root.Exists)) {
Write-Host -ForegroundColor Red 'Could not find $Root folder
'
exit 2
}
$AllItems = Get-ChildItem -Path $Root -Recurse -Force -ErrorAction SilentlyContinue
} else {
Write-Host -ForegroundColor Red '$AllUsers needs to be $true or $false
'
exit 2
}
#Find only files/folders that are over the $MaxLength
$LongItems = $AllItems | Where-Object {$_.FullName.Length -gt $MaxLength}
#Report findings
if ($Null -eq $LongItems) {
Write-Host -ForegroundColor Green 'No long paths found
'
} else {
$LongItems | Sort-Object -Property FullName | Format-Table -Property FullName -HideTableHeaders
}
}
Write-Host 'Long Paths Enabled: ' -NoNewline
switch ($LongPathsKey.LongPathsEnabled) {
0 {
Write-Host -ForegroundColor Yellow 'No'
}
1 {
Write-Host -ForegroundColor Green 'Yes'
}
Default {
Write-Host -ForegroundColor Red 'Key missing'
}
}
#If $AskToSetLongPaths = $true ask to change registry key
if ($null -ne $LongPathsKey.LongPathsEnabled) {
if ($AskToSetLongPaths -eq $true) {
Write-Host '
0 = Disable
1 = Enable
2 = Skip
'
do {
$EnableLongPaths = Read-Host -Prompt 'Enable long paths?'
if (($EnableLongPaths -notin 0..2) -or ($null -eq $EnableLongPaths) -or ($EnableLongPaths.Length -lt 1)) {
Write-Host -ForegroundColor Red 'Enter 0,1,or 2 only'
}
} until (
(($EnableLongPaths -in 0..2) -and ($null -ne $EnableLongPaths) -and ($EnableLongPaths.Length -gt 0))
)
Write-Host ''
if ($EnableLongPaths -eq $LongPathsKey.LongPathsEnabled) {
Write-Host -ForegroundColor Yellow 'Selection already matches registry value
'
} else {
if ($EnableLongPaths -eq 1) {
Set-ItemProperty -Path $LongPathsKey.PSPath -Name 'LongPathsEnabled' -Value 1
$LongPathsKey = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem')
if ($LongPathsKey.LongPathsEnabled -ne 1) {
Write-Host -ForegroundColor Red 'Registry key was not changed
'
} else {
Write-Host -ForegroundColor Green 'Registry key was changed
'
}
} elseif ($EnableLongPaths -eq 0) {
Set-ItemProperty -Path $LongPathsKey.PSPath -Name 'LongPathsEnabled' -Value 0
$LongPathsKey = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem')
if ($LongPathsKey.LongPathsEnabled -ne 0) {
Write-Host -ForegroundColor Red 'Registry key was not changed
'
} else {
Write-Host -ForegroundColor Green 'Registry key was changed
'
}
} elseif ($EnableLongPaths -eq 2) {
Write-Host -ForegroundColor Yellow 'Skipped adjusting registry key
'
} else {
Write-Host -ForegroundColor Red '$EnableLongPaths is not 0,1,or 2
'
}
}
} else {
Write-Host ''
}
}
#Report errors if any
if ($Error.Count -gt 0) {
#Write-Host -ForegroundColor Red 'Errors detected, review $Error'
}