2

I would very much like to add the "Recycle Bin" to the "Quick access" area of File Explorer using PowerShell (as this is a standard thing that I like to have on all new systems). Adding to "Quick access" is simple:

$oShell = New-Object -ComObject Shell.Application
$oShell.Namespace("C:\ProgramData\chocolatey\lib").Self.InvokeVerb("PinToHome")

The problem is that the "Recycle Bin" is not a normal folder of course. It can however be accessed as follows:

$oShell = New-Object -ComObject Shell.Application
$rb = $oShell.Namespace(10)

But adding to "Quick access" with $rb.Self.InvokeVerb("PinToHome") does not work.

Please can someone show me how to programmatically add the "Recycle Bin" onto "Quick access". I know that this can be done, as dragging the item from the Desktop onto File Explorer works, so there must be some programmable way to achieve this also?

YorSubs
  • 1,087

2 Answers2

1

This is really not a PS code issue, nor a programmatic one. It's an OS-specific limitation/impediment.

Windows Explorer is a program and thus has features it directly supplies which may or may not be available outside of it.

If Microsoft does not provide a handle to do this, then, it requires much hacking relevant to Windows Explorer to discover it.

I personally cannot see a use case for this pinning effort, since it is already available from your desktop, Start Menu and one can pin to the Taskbar for quick access, but we all have our reasons for X or Y thingy/approach.

When you dig at Windows Explorer features. For example, as documented here: Shell.Explore,Method; you can determine from this, that in Windows Explorer, one can just do stuff like this in the path input to get to folder times. shell:sendto.

Yet, if you tried that for Recycle, shell:desktop\Recycle Bin then that would fail, for the reason already noted. So, you need to dig further into such docs for any other possibilities. RB is just a .lnk to an app call.

Even as noted by your post:

$oShell = New-Object -ComObject Shell.Application
$rb = $oShell.Namespace(10)

If you go deeper ...

$oShell = New-Object -ComObject Shell.Application
(($rb = $oShell.Namespace(10))).ParentFolder
# Results
<#
Title                      : Desktop
Application                : System.__ComObject
Parent                     : 
ParentFolder               : 
Self                       : System.__ComObject
OfflineStatus              : 
HaveToShowWebViewBarricade : False
ShowWebViewBarricade       : False
#>

You can see the issue with your use case.

For all the Windows Explorer special folders, then there are these:

[Enum]::GetNames('System.Environment+SpecialFolder')
# Results
<#
Desktop
Programs
MyDocuments
Personal
Favorites
Startup
Recent
SendTo
StartMenu
MyMusic
MyVideos
DesktopDirectory
MyComputer
NetworkShortcuts
Fonts
Templates
CommonStartMenu
CommonPrograms
CommonStartup
CommonDesktopDirectory
ApplicationData
PrinterShortcuts
LocalApplicationData
InternetCache
Cookies
History
CommonApplicationData
Windows
System
ProgramFiles
MyPictures
UserProfile
SystemX86
ProgramFilesX86
CommonProgramFiles
CommonProgramFilesX86
CommonTemplates
CommonDocuments
CommonAdminTools
AdminTools
CommonMusic
CommonPictures
CommonVideos
Resources
LocalizedResources
CommonOemLinks
CDBurning
#>

... and note that Quick Access and the like is not one of them. So, shell:, is not an option for it as well as for several others in that list.

Update

So, as per my comment... You can manipulate anything you create. ...to your GUID use comment. You can then just do this:

New-Item -Path 'C:\' -Name Temp -ItemType Directory -Force
New-Item -Path 'C:\temp' -Name 'Recycle Bin.{645FF040-5081-101B-9F08-00AA002F954E}' -ItemType Directory
($TargetShellObject = (Get-ChildItem -Path 'C:\Temp' -Filter 'Recycle*').FullName)
$oShell = New-Object -ComObject Shell.Application
$oShell.Namespace("$TargetShellObject").Self.InvokeVerb("PinToHome")

This will put that new directory on the Quick Access list, by name only without the GUID, until you look at its properties. However, guess what happens if you try and clean up/remove that manually created folder? The moment you close and restart Windows Explorer, it is gone, along with the pin in QA.

So, that means once you create that folder object, it can never be removed. So, to keep others from deleting it, you must hide or protect it. Thus, stuff like this...

New-Item -Path 'C:\' -Name 'QuickAccessObjects' -ItemType Directory -Force
New-Item -Path 'C:\QuickAccessObjects' -Name 'Recycle Bin.{645FF040-5081-101B-9F08-00AA002F954E}' -ItemType Directory

(Get-Item -Path 'C:\QuickAccessObjects').attributes = 'Hidden'

($TargetShellObject = (Get-ChildItem -Path 'C:\QuickAccessObjects' -Filter 'Recycle*').FullName)

$oShell = New-Object -ComObject Shell.Application $oShell.Namespace("$TargetShellObject").Self.InvokeVerb("PinToHome")

enter image description here

Update

As per the comment. Using the $env:windir, I get the same. No GUID at all regardless of view settings, save the Tiles one which still only shows the path.

enter image description here As demo'd, this is all done in the Windows Sandbox pristine environment.

postanote
  • 5,136
1

Src: Pin Recycle Bin to Quick Access using InvokeVerb

Try this:

$RBPath = 'HKCU:\Software\Classes\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\shell\pintohome\command\'
$name = "DelegateExecute"
$value = "{b455f46e-e4af-4035-b0a4-cf18d2f6f28e}"
New-Item -Path $RBPath -Force | out-null
New-ItemProperty -Path $RBPath -Name $name -Value $value -PropertyType String -Force | out-null
$oShell = New-Object -ComObject Shell.Application
$trash = $oShell.Namespace("shell:::{645FF040-5081-101B-9F08-00AA002F954E}")
$trash.Self.InvokeVerb("PinToHome")
Remove-Item -Path "HKCU:\Software\Classes\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}" -Recurse
w32sh
  • 12,379