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
  • 669
  • 7
  • 23

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
  • 4,589
  • 2
  • 7
  • 7
  • You can drag Recycle Bin onto Quick access, so while we can't (yet) see the exposed method to do it, there is a way to do it within the OS. Further, I found out today that you can create a folder with the name `Recycle Bin.{645FF040-5081-101B-9F08-00AA002F954E}` and then you can pin that folder to Quick access (you can automate all of this with PowerShell, *but* you are left with a big ugly GUID on the pinned item; there is a way to hide all *folder* extensions but that's overkill as it applies to all folders everywhere). We're very close here I think. – YorSubs Sep 18 '22 at 07:05
  • 1
    Note, I never said you could not drag and drop, but was responding to your programmatic request as written. There are many folders to Windows System spaces you can get at via a GUID, much like the most talked about [God Mode](https://www.lifewire.com/god-mode-windows-4154662#:~:text=How%20to%20Activate%20God%20Mode%20in%20Windows%201,the%20new%20folder%20to%20see%20GodMode%20in%20action.) approach, just as you are doing with RB. Yet, as you have just noted, ***you are creating a new folder object*** not using an existing one, as per your post request. You can manipulate anything you create. – postanote Sep 18 '22 at 07:24
  • Sure, I addressed drag and drop just in terms of noting that it is doable (we want to automate it of course, manual click-click is never good to setup a system to a desired state). Yes, same principle as God Mode. Your update is how I've been doing it, but this shows with the GUID for me, I guess because I've turned off "Hide extensions for known file types" (which I can't live without really). Any automation that gets a clean Recycle Bin onto the Quick access is good (I just mentioned GUID as I found that *partially* works today). Still very close. – YorSubs Sep 18 '22 at 08:52
  • Good point on "once you create that folder object, it can never be removed.". I guess I would be inclined to just create it in an existing immutable location that, such as `C:\Windows` or better, `C:\Program Files\Reference Assemblies` where it is then effectively hidden. Just can't get rid of that GUID part ... might a soft or hard link to our GUID folder work to provide a means that item that is on Quick access has a name without the GUID do you think? – YorSubs Sep 18 '22 at 08:59
  • 1
    I don't get the GUID at all. I just the path to the parent folder, and only when f I have the root Quick Access item selected, thus showing all Frequent Folders group. Now, this only occurs if I am in a Tile view. All other views it does not show that path or the GUID, including whether or not if I have the "hidden items or file name extension" enabled or not. What OS are you running, or targeting? – postanote Sep 18 '22 at 09:11
  • I'm just running Windows 10. 21H2. I also have "Hide protected operating system files" disabled (though that's about hiding things completely, so probably not the cause). I'm trying this both in my normal host and in a Windows Sandbox that I spin up (which should be a pristine image of the OS) and I see the GUID in both. – YorSubs Sep 18 '22 at 09:24
  • I'm always loathe to make firm statements like "It's an OS-specific limitation/impediment." as I often find out that there is in fact a way of doing a thing. See the answer, it's quite impressive / interesting. Thanks for your notes on this; all very useful also. – YorSubs Sep 18 '22 at 12:26
  • Yep, an interesting way to resolve. This is apparently someone who was tracking us on this thread and verses piping it in here, spent the time digging at this, and wrote a full blog on the topic. That's an assumption, of course, c/o the post being 18 Sept 22 as our thread continued. Still and OS native impedance, and as demo'd by the approaches, ***one has to create objects (file system and or registry) to get around it (barring an OS patch killing it later)***. However, this, though it required more digging, is far more elegant than the path we were on. ;-} – – postanote Sep 18 '22 at 21:52
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
  • 11,524
  • 2
  • 39
  • 44