forked from LesFerch/WinSetView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinSetView.ps1
361 lines (289 loc) · 12.6 KB
/
WinSetView.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# WinSetView (Globally Set Explorer Folder Views)
# Les Ferch, [email protected], GitHub repository created 2021-03-26
# WinSetView.ps1 (Powershell script to set selected views)
# One command line paramater is supported
# Provide INI file name to set Explorer views
# Provide REG file name to restore Explorer views from backup
Param (
$File = ''
)
$host.ui.RawUI.WindowTitle = "WinSetView"
# Read entire INI file into a dictioonary object
Function Get-IniContent ($FilePath) {
$ini = @{}
Switch -regex -file $FilePath {
'^\[(.+)\]' # Section
{
$section = $matches[1]
$ini[$section] = @{}
}
'(.+?)\s*=(.*)' # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
Return $ini
}
# Get Windows major version from ProductName value
# Treat Windows 11 same as Windows 10 because they
# have same folder types and properties
$Key = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion'
$Value = 'ProductName'
$WinVer = (Get-ItemProperty -Path $Key -Name $Value).$Value.SubString(8,2)
$WinVer = ' .' + $WinVer + '. '
$WinVer = ($WinVer -Replace '\.',' ').Trim()
If ($WinVer -eq '11') {$WinVer = '10'}
If (($WinVer -ne '7') -And ($WinVer -ne '8') -And ($WinVer -ne '10')){
Write-Host `n'Windows 7, 8, 10 or 11 is required.'`n
Read-Host -Prompt 'Press any key to continue'
Exit
}
# Windows 7 includes Powershell 2 so this check should never fail
If ($PSVersionTable.PSVersion.Major -lt 2) {
Write-Host `n'Powershell 2 or higher is required.'`n
Read-Host -Prompt 'Press any key to continue'
Exit
}
# Verify INI or REG file is supplied on the command line
$FileExt = ''
If ($File.Length -gt 4) {$FileExt = $File.SubString($File.Length-4)}
If (-Not(($FileExt -eq '.ini') -Or ($FileExt -eq '.reg'))) {
Write-Host `n'No INI or REG file supplied on command line.'`n
Read-Host -Prompt 'Press any key to continue'
Exit
}
If (-Not(Test-Path -Path $File)) {
Write-Host `n"File not found: $File"`n
Read-Host -Prompt 'Press any key to continue'
Exit
}
$BagM = '"HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\BagMRU"'
$Bags = '"HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags"'
$Defs = '"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Defaults"'
$CUFT = '"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes"'
$LMFT = '"HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes"'
$Advn = '"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"'
$ImpR = '[HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\AllFolders\'
$TempDir = "$env:TEMP"
$AppData = "$env:APPDATA\WinSetView"
$RegFile1 = "$TempDir\WinSetView1.reg"
$RegFile2 = "$TempDir\WinSetView2.reg"
$T1 = "$TempDir\WinSetView1.tmp"
$T2 = "$TempDir\WinSetView2.tmp"
$T3 = "$TempDir\WinSetView3.tmp"
$T4 = "$TempDir\WinSetView4.tmp"
$TimeStr = (get-date).ToString('yyyy-MM-dd-HHmm-ss')
# Create PSScriptRoot variable if not exist (i.e. PowerShell 2)
If (!$PSScriptRoot) {$PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path -Parent}
# Use script folder if we have write access. Otherwise use AppData folder.
$TestFile = "$PSScriptRoot\$TimeStr.txt"
Try {[io.file]::OpenWrite($TestFile).close()}
Catch {}
If (Test-Path -Path $TestFile) {
Remove-Item $TestFile
$AppData = "$PSScriptRoot\AppData"
}
$BakFile = "$AppData\Backup\$TimeStr.reg"
$Custom = "$AppData\WinSetViewCustom.reg"
Function RestartExplorer {
Get-process explorer | Stop-Process
Explorer $PSScriptRoot
Exit
}
Function DeleteUserKeys {
Reg Delete $BagM /f 2>$Null
Reg Delete $Bags /f 2>$Null
Reg Delete $CUFT /f 2>$Null
If ($KeepViews -eq 0) {Reg Delete $Defs /f 2>$Null}
}
If ($FileExt -eq '.reg') {
Write-Host `n'Restore from backup...'`n
}
Else {
$iniContent = Get-IniContent $File
$Reset = $iniContent['Options']['Reset']
$KeepViews = [Int]$iniContent['Options']['KeepViews']
$Generic = [Int]$iniContent['Options']['Generic']
$SearchOnly = [Int]$iniContent['Options']['SearchOnly']
$SetVirtualFolders = [Int]$iniContent['Options']['SetVirtualFolders']
$FileDialogOption = [Int]$iniContent['Options']['FileDialogOption']
$FileDialogView = [Int]$iniContent['Options']['FileDialogView']
$FileDialogNG = [Int]$iniContent['Options']['FileDialogNG']
$ThisPCoption = [Int]$iniContent['Options']['ThisPCoption']
$ThisPCView = [Int]$iniContent['Options']['ThisPCView']
$ThisPCNG = [Int]$iniContent['Options']['ThisPCNG']
If ($Reset -eq 1) {
Write-Host `n'Reset to Windows defaults...'`n
}
Else {
Write-Host `n'Setting Explorer folder views...'`n
}
}
# Create Exlorer folder view backup reg file
If (!(Test-Path -Path "$AppData\Backup")) {Mkdir "$AppData\Backup" >$Null}
Remove-Item $T1 2>$Null
Remove-Item $T2 2>$Null
Remove-Item $T3 2>$Null
Remove-Item $T4 2>$Null
Reg Export $BagM $T1 /y 2>$Null
Reg Export $Bags $T2 /y 2>$Null
Reg Export $Defs $T3 /y 2>$Null
Reg Export $CUFT $T4 /y 2>$Null
Cmd /c Copy $T1+$T2+$T3+$T4 $BakFile >$Null 2>$Null
Remove-Item $T1 2>$Null
Remove-Item $T2 2>$Null
Remove-Item $T3 2>$Null
Remove-Item $T4 2>$Null
# Clear current Explorer view registry values
DeleteUserKeys
# Restore from backup, restart Explorer, exit
If ($FileExt -eq '.reg') {
Reg Import $File
RestartExplorer
}
# Set option to hide or show file extensions
$ShowExt = [Int]$iniContent['Options']['ShowExt']
Reg Add $Advn /v HideFileExt /t REG_DWORD /d (1-$ShowExt) /f
# Set Windows 11 option to enable or disable compact view
# This value is ignored on older Windows versions
$CompView = [Int]$iniContent['Options']['CompView']
Reg Add $Advn /v UseCompactMode /t REG_DWORD /d ($CompView) /f
# If reset, restart Explorer and exit
If ($Reset -eq 1) {RestartExplorer}
# Function to help Set up views for This PC and Network virtual folders
Function SetBagValues ($Key) {
Reg Add $Key /v LogicalViewMode /d $LVMode /t REG_DWORD /f >$Null
Reg Add $Key /v Mode /d $Mode /t REG_DWORD /f >$Null
If ($ThisPCNG -eq 1) {Reg Add $Key /v GroupView /d 0 /t REG_DWORD /f >$Null}
If ($LVMode -eq 3) {Reg Add $Key /v IconSize /d $IconSize /t REG_DWORD /f >$Null}
}
# Set view values based on selection index
Function SetViewValues($Index) {
If ($Index -eq 1) {$Script:LVMode = 1; $Script:Mode = 4}
If ($Index -eq 2) {$Script:LVMode = 4; $Script:Mode = 3}
If ($Index -eq 3) {$Script:LVMode = 2; $Script:Mode = 6}
If ($Index -eq 4) {$Script:LVMode = 5; $Script:Mode = 8}
If ($Index -eq 5) {$Script:LVMode = 3; $Script:Mode = 1; $Script:IconSize = 16}
If ($Index -eq 6) {$Script:LVMode = 3; $Script:Mode = 1; $Script:IconSize = 48}
If ($Index -eq 7) {$Script:LVMode = 3; $Script:Mode = 1; $Script:IconSize = 96}
If ($Index -eq 8) {$Script:LVMode = 3; $Script:Mode = 1; $Script:IconSize = 256}
}
Function BuildRegData($Key) {
$Script:RegData += $Script:ImpR + "$Key\$GUID]`r`n"
$Script:RegData += '@="' + $Script:FT + '"' + "`r`n"
$Script:RegData += '"LogicalViewMode"=dword:' + $Script:LVMode + "`r`n"
$Script:RegData += '"Mode"=dword:' + $Script:Mode + "`r`n"
If ($Script:LVMode -eq 3) {$Script:RegData += '"IconSize"=dword:' + $Script:IconSize + "`r`n"}
}
# Set up views for This PC and Network virtual folders
If ($ThisPCoption -ne 0) {
# The FolderTypes key does not have default values for This PC and Network,
# so we will set them with initial values in the BagMRU and Bags registry keys.
Reg Add "$BagM" /v NodeSlots /d '020202' /t REG_BINARY /f
Reg Add "$BagM" /v MRUListEx /d '0100000000000000ffffffff' /t REG_BINARY /f >$Null
Reg Add "$BagM" /v '0' /d '14001F50E04FD020EA3A6910A2D808002B30309D0000' /t REG_BINARY /f >$Null
Reg Add "$BagM" /v '1' /d '14001F580D1A2CF021BE504388B07367FC96EF3C0000' /t REG_BINARY /f >$Null
Reg Add "$BagM\0" /v NodeSlot /d 1 /t REG_DWORD /f >$Null
Reg Add "$BagM\1" /v NodeSlot /d 2 /t REG_DWORD /f >$Null
SetViewValues($ThisPCView)
$GUID = '{5C4F28B5-F869-4E84-8E60-F11DB97C5CC7}'
SetBagValues("$Bags\1\ComDlg\$GUID")
SetBagValues("$Bags\1\Shell\$GUID")
If (!($Generic -eq 1)) {$GUID = '{25CC242B-9A7C-4F51-80E0-7A2928FEBE42}'}
SetBagValues("$Bags\2\ComDlg\$GUID")
SetBagValues("$Bags\2\Shell\$GUID")
}
If ($Generic -eq 1) {Reg Add "$Bags\AllFolders\Shell" /v FolderType /d Generic /t REG_SZ /f}
If ($SetVirtualFolders -eq 1) {
$GUID = $iniContent['Generic']['GUID']
$GroupBy = $iniContent['Generic']['GroupBy']
SetViewValues([Int]$iniContent['Generic']['View'])
Reg Add "$Bags\AllFolders\Shell\$GUID" /v Mode /d "$Mode" /t REG_DWORD /f
Reg Add "$Bags\AllFolders\Shell\$GUID" /v LogicalViewMode /d "$LVMode" /t REG_DWORD /f
If ($LVMode -eq 3) {Reg Add "$Bags\AllFolders\Shell\$GUID" /v IconSize /d "$IconSize" /t REG_DWORD /f}
If ($GroupBy -eq '') {Reg Add "$Bags\AllFolders\Shell\$GUID" /v GroupView /d 0 /t REG_DWORD /f}
}
# Set Explorer folder view defaults:
# 1) Export HKLM FolderTypes key
# 2) Import FolderTypes key to HKCU
# 2) Update HKCU FolderTypes default settings
# 4) Restart Explorer
# Note: Explorer will use HKCU key instead of HKLM key automatically
# Copy FolderType key from HKLM to HKCU by exporting and importing
Reg Export $LMFT $RegFile1 /y
$Data = Get-Content $RegFile1
$Data = $Data -Replace 'HKEY_LOCAL_MACHINE','HKEY_CURRENT_USER'
Out-File -InputObject $Data -encoding Unicode -filepath $RegFile1
Reg Import $RegFile1
$FolderTypes = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes'
# Create lookup table for path type properties so these can be excluded from
# non-search views if the search-only option is enabled
$PathItems = @{
'ItemFolderPathDisplay' = ''
'ItemFolderPathDisplayNarrow' = ''
'ItemPathDisplay' = ''
'ItemFolderNameDisplay' = ''
}
If ($FileDialogOption -eq 1) {$RegData = "Windows Registry Editor Version 5.00`r`n`r`n"}
Get-ChildItem $FolderTypes | Get-ItemProperty | ForEach {
$FT = $_.CanonicalName
If ($iniContent.ContainsKey($FT)) {
$Include = [Int]$iniContent[$FT]['Include']
If ($Include -eq 1) {
If (($FileDialogOption -eq 1) -And ($FT -ne 'Global')) {
$GUID = $iniContent[$FT]['GUID']
SetViewValues($FileDialogView)
BuildRegData('ComDlg')
BuildRegData('ComDlgLegacy')
}
$GroupBy = $iniContent[$FT]['GroupBy']
If ($GroupBy -ne '') {$GroupBy = "System.$GroupBy"}
$SortBy = 'prop:' + $iniContent[$FT]['SortBy']
$SortBy = $SortBy -Replace '\+','+System.'
$SortBy = $SortBy -Replace '-','-System.'
$View = $iniContent[$FT]['View']
$CustomIconSize = $iniContent[$FT]['IconSize']
SetViewValues($View)
If ($CustomIconSize -ne '') {$IconSize = $CustomIconSize}
$INIColumnList = $iniContent[$FT]['ColumnList']
$ArrColumnList = $INIColumnList.Split(';')
$ColumnList = 'prop:'
For($i=0; $i -lt $ArrColumnList.Count; $i++) {
$ArrColumnListItem = $ArrColumnList[$i].Split(',')
$Property = $ArrColumnListItem[2]
If (($FT -Match 'Search') -Or (-Not ((($SearchOnly -eq 1) -And ($PathItems.ContainsKey($Property))) -Or ($Property -eq 'Search.Rank')))) {
$Show = $ArrColumnListItem[0]
$Width = ''; If ($ArrColumnListItem[1] -ne '') {$Width = '(' + $ArrColumnListItem[1] + ')'}
$Property = 'System.' + $ArrColumnListItem[2]
$ColumnList = "$ColumnList$Show$Width$Property;"
}
}
$ColumnList = $ColumnList.Trim(';')
$Key = $_.PSPath
$Key = "$Key\TopViews"
If (Test-Path -Path $Key) {
Get-ChildItem $Key | Get-ItemProperty | ForEach {
$GUID = $_.PSChildName
$ChildKey = $Key + '\' + $GUID
Set-ItemProperty -Path $ChildKey -Name 'LogicalViewMode' -Value $LVMode
If ($LVMode -eq 3) {Set-ItemProperty -Path $ChildKey -Name 'IconSize' -Value $IconSize}
Set-ItemProperty -Path $ChildKey -Name 'GroupBy' -Value $GroupBy
Set-ItemProperty -Path $ChildKey -Name 'SortByList' -Value $SortBy
Set-ItemProperty -Path $ChildKey -Name 'ColumnList' -Value $ColumnList
}
}
}
}
}
# Export results for use with comparison tools such as WinDiff
Reg Export $CUFT $RegFile2 /y
# Import Reg data to force dialog views
# This is MUCH faster than creating the keys using PowerShell
If ($FileDialogOption -eq 1) {
Out-File -InputObject $RegData -filepath $T1
Reg Import $T1
}
# Import Reg data for any custom settings
If (Test-Path -Path $Custom) {Reg Import $Custom}
RestartExplorer