Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Pin To Start Special Folders With No Target

How do you pin to start special folders using powershell? Like "ThisPC", iexplorer

This will pin to start exe's fine, but what about windows explorer and myComputer? How to pin those items since they have no target?

Given this

 <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%APPDATA%\Microsoft\Windows\Start Menu\Programs\Windows System\This PC.lnk" />

It seems to have issues with .lnk's for "This PC", "File Explorer", etc

Function PinLnk
{
    Param
    (
        [Parameter(Mandatory,Position=0)]
        [Alias('p')]
        [String[]]$Path
    )
    $Shell = New-Object -ComObject Shell.Application
    $Desktop = $Shell.NameSpace(0X0)
    $WshShell = New-Object -comObject WScript.Shell
    $Flag=0
    Foreach($itemPath in $Path)
    {
        $itemName = Split-Path -Path $itemPath -Leaf
        #pin application to windows Start menu
        $ItemLnk = $Desktop.ParseName($itemPath)
        $ItemVerbs = $ItemLnk.Verbs()
        Foreach($ItemVerb in $ItemVerbs)
        {
            If($ItemVerb.Name.Replace("&","") -match "Pin to Start")
            {
                $ItemVerb.DoIt()
                $Flag=1
            }
        }
    }
}
PinLnk "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"

I tried this approach as well still not pinning mycomputer to start

PS C:\WINDOWS\system32> Function PinLnk14
>> {
>>
>> $shell = new-object -com Shell.Application
>> $folder = $shell.NameSpace("shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") # ssfDRIVES
>> $ItemVerbs=$folder.Self.Verbs()
>>         Foreach($ItemVerb in $ItemVerbs)
>>         {
>>             Write-Output $ItemVerb.Name
>>             If($ItemVerb.Name.Replace("&","") -match "Pin to Start")
>>             {
>> Write-Output "TRYING TO PIN"
>>                 $ItemVerb.DoIt()
>>             }
>>         }
>>     }
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Pinlnk14
&Open
Pin to Quick access
Mana&ge
&Pin to Start
TRYING TO PIN
Map &network drive...
Dis&connect network drive...
Create &shortcut
&Delete
Rena&me
P&roperties
like image 339
nlstack01 Avatar asked Aug 14 '17 20:08

nlstack01


People also ask

Can you Pin a specific folder to the taskbar?

If you right click on the Taskbar | Toolbars and check Quick Launch, you can drag a folder to that section of the Taskbar.

How do I pin a folder to the Start menu?

Pinning a Folder to Windows 10 Start is easy, the operating system already offers this context menu item. Right-click on any folder and you will see Pin to Start. Click on it to pin the folder to Start.

How to Pin a folder to Desktop Windows 10?

Press and hold (or right-click) the desktop, then select New > Shortcut. Enter the location of the item or select Browse to find the item in File Explorer, then select the location and select OK. Select Next, then select Finish.

How do I pin something to quick access?

You can set a folder to show up in Quick access so it'll be easy to find. Just right-click it and select Pin to Quick access. Unpin it when you don't need it there anymore. If you want to see only your pinned folders, you can turn off recent files or frequent folders.


1 Answers

Most special folders are accessible using special values, documented here: ShellSpecialFolderConstants enumeration

So, if you want to get the My Computer (a.k.a. "This PC") folder, you can do this:

$shell = new-object -com Shell.Application
$folder = $shell.NameSpace(17) # "ssfDRIVES" constant

And this will get you a Folder object.

There is another way wich uses the folder CLSID (a guid). It will allow you to get to any folder in what's called the shell namespace, even the ones that may not be defined in the enumeration above (3rd party shell namespace extensions for example). The syntax is this:

$shell = new-object -com Shell.Application
$folder = $shell.Namespace("shell:::{CLSID}")

In fact, this funny syntax combines the 'shell:' URI moniker with the shell namespace parsing syntax ::{CLSID}.

So for example to get the My Computer folder, you would do use the constant known as CLSID_MyComputer like this:

$shell = new-object -com Shell.Application
$folder = $shell.Namespace("shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")

This will also work:

$shell = new-object -com Shell.Application
$folder = $shell.Namespace("shell:MyComputerFolder") # direct shell: syntax

And it should bring you back the same object as in the previous call. They're all equivalent.

Once you have a Folder object, there is a last trick to get the associated verbs, because the Verbs() method is only defined on the FolderItem object. To get the FolderItem from the Folder (as a Folder is also an "item" in the namespace, so it has also a FolderItem facade), you can use the Self property, like this:

$shell = new-object -com Shell.Application
$folder = $shell.NameSpace("shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") # ssfDRIVES
$folder.Self.Verbs()

Ok, that was to get verbs for a FolderItem. To pin an item to start however, the "Pin to Start" verb invocation does not always work (for My Computer for example), or the verb isn't even available (for a standard folder for example). In general, it doesn't really work well for folders for some reason.

So, one solution for folders is to first create a shortcut file (.lnk) somewhere to that folder (including My Computer or other special locations), and pin that shortcut file to start. Note: the standard (non language localized) verb for Pin to Start is "PinToStartScreen", it's better to use that than to scan various verbs (all verbs have a canonical name). So the code would look like this to pin My Computer to start:

$wshell = new-object -com WScript.Shell
$shortcut = $wshell.CreateShortcut("c:\temp\mypc.lnk")
$shortcut.TargetPath = "shell:MyComputerFolder" # use the same syntax as described above
$shortcut.Save()

$shell = new-object -com Shell.Application
$folder = $shell.NameSpace("c:\temp")
$lnk = $folder.ParseName("mypc.lnk") # get the shortcut file
$lnk.InvokeVerb("PinToStartScreen") # invoke "Pin To Start" on the shortcut file

The fact is that's exactly what Windows does when we do "Pin to Start" on My Computer, it creates a shortcut in C:\Users\<my user>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

like image 161
Simon Mourier Avatar answered Oct 10 '22 11:10

Simon Mourier