OK, here's version 1.0 of Set_Saved_Search_View_As_Default. When you run the code, you'll be presented with a GridVIvew control listing your saved
SearchResults
views, along with the FolderType used. All the common FolderTypes have a corresponding
SearchResults
type with the exception of
Downloads, which uses its base type for search results as well. You have to set a default for each type to override its default. Possible search result types are:
Downloads
Contacts.SearchResults
Documents.SearchResults
OtherUsers.SearchResults
Pictures.SearchResults
Communications.SearchResults
UsersLibraries.SearchResults
Music.SearchResults
Generic.SearchResults
PublishedItems.SearchResults
UserFiles.SearchResults
Videos.SearchResults
Though it's unlikely most users will ever see all of those. In the GridView, select the folder(s) you want copy as a template for its FolderType. You can only have one template for each type, if you select a type that already has an entry under AllFolders, or if you select more than one of the same type, the last one written will be the one to survie.
Attachment 321460
Select the Folders/Types and click
OK.
PowerShell will display confimation for each view copied:
Code:
. . .
>> '@ -f $Splat )
>> }
>>
Default view set for: Pictures.SearchResults
Based on: "Search Results in Spring 2015" ( Bag #949 )
Key Name: HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\{4DCAFE13-E6A7-4C28-BE02-CA8C2126280D}
Copy & paste directly into
PowerShell or save as a
.ps1
file.
Code:
### String constans
$ShellKey = 'HKCU:\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell'
$BagMRU = "$ShellKey\BagMRU"
$Bags = "$ShellKey\Bags"
$ConfirmCopy = @'
Default view set for: {0}
Based on: "{1}" ( Bag #{2} )
Key Name: {3}
'@
### Dictonary FOlderTYpeID -> FolderType.CanonicalName
$FTKey = Get-Item 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes'
$FT_Lookup = $FTKey.GetSubkeyNames() | ForEach{ $hash = @{'{25CC242B-9A7C-4F51-80E0-7A2928FEBE42}'='Network'} } {
$hash.Add( $_, $FTKey.OpenSubkey($_).GetValue('CanonicalName') )
} { $hash }
$Shell = New-Object -ComObject shell.application
(Get-Item $BagMRU).Property -match '\d+' | ForEach{
Try{
[PSCustomObject]@{
'Bag' = Get-ItemPropertyValue "$BagMRU\$_" NodeSlot
'Name' = ( $Shell.NameSpace(( Get-ItemPropertyValue $BagMRU $_ ))).Title
}
} Catch {}
} | Where Name -match 'Search Results' |
Where { (Test-Path "$Bags\$($_.Bag)\Shell") -and (Get-Item "$Bags\$($_.Bag)\Shell").SubKeyCount } | ForEach{
$Splat = @{
'NotePropertyName' = 'Type'
'NotePropertyValue' = $FT_Lookup[( Get-Item "$Bags\$($_.Bag)\Shell" ).GetSubkeyNames()]
'PassThru' = $True
}
$_ | Add-Member @Splat
} | Group Name | ForEach {
[PSCustomObject]@{
'Name' = $_.Name
'Bag' = $_.Group.Bag
'Type' = $_.Group.Type | Select -Unique
}
### *** Launch GridView for folder selection ***
} | Out-GridView -PassThru | ForEach{
### Confirm copy
$Splat = @(
$_.Type
$_.Name
$_.Bag
( Copy-Item -Path "$Bags\$($_.Bag)\Shell\*" -Destination "$Bags\AllFolders\Shell" -Recurse -Force -PassThru ).Name.Replace( 'HKEY_CURRENT_USER' , 'HKCU' )
)
Write-Output ( $ConfirmCopy -f $Splat )
### Apply to existing?
If ( ( Read-Host 'Apply this template to any currenly saved views of the same type? (Y/N)' ) -match '^y') {
Get-ChildItem $Bags -Recurse -Depth 2 |
Where PSChildName -eq $Splat[3].Split('\')[-1] |
Where Name -notMatch AllFolders |
Remove-Item -Recurse -Force
}
}