How I pin a lnk to start screen only in powershell

Gerrits, Marc (M.) 5 Reputation points
2024-12-17T07:27:07.4033333+00:00

Hi Team
I am the application developer on Ford provingground.
I get the job to automate deployment on tablets on my side.
The only issue that a have is to pin link to the start menu with powershell, but I did all sort of thing.
But it don't work, do you have some code snippet how to do it.
And is this still supperted with powershell.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,951 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,401 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,725 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,496 Reputation points
    2024-12-19T16:20:17.8666667+00:00

    If you limit the scope just Windows:

    $W ScriptShell = New-Object -ComObject W Script.Shell 
    $shortcut = $W ScriptShell.CreateShortcut($shortcutPath) 
    $shortcut.TargetPath = $targetPath 
    $shortcut.Save()
    

    NOTE: For some reason I had to change the "WScript..." to "W Script" before I was able to get this answer to be accepted by the forum editor. Remove the space (on the 1st and 2nd lines) before you try running the code.


  2. Rich Matheisen 47,496 Reputation points
    2024-12-20T16:36:19.09+00:00

    I found this:
    https://stackoverflow.com/questions/16233165/how-to-pin-application-icon-to-the-metro-start-screen-in-windows-8-programmatica?rq=4

    It's for Windows 8, but it's for the "Metro" style of the start menu. It may still work.

    If you'd like to try it, here's the PowerShell version (UNTESTED!!):

    function PinToStart {
        param (
            [string]$filePath
        )
        $success = $false
        # Break into file name and path
        $directoryName = [System.IO.Path]::GetDirectoryName($filePath)
        $fileName = [System.IO.Path]::GetFileName($filePath)
        # Load shell32.dll
        $shell32 = Add-Type -MemberDefinition @"
        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        public static extern int LoadString(IntPtr hInstance, int uID, System.Text.StringBuilder lpBuffer, int nBufferMax);
    "@ -Name "Shell32" -Namespace "Win32" -PassThru
        $hShell32 = [System.Runtime.InteropServices.Marshal]::GetHINSTANCE([System.Reflection.Assembly]::GetExecutingAssembly().GetModules()[0])
        if ($hShell32 -ne [IntPtr]::Zero) {
            # Get the localized translation of 'Pin to Start' verb
            $szPinToStartLocalized = New-Object System.Text.StringBuilder 256
            $nPinToStartLocalizedLength = [Win32.Shell32]::LoadString($hShell32, 51201, $szPinToStartLocalized, $szPinToStartLocalized.Capacity)
            if ($nPinToStartLocalizedLength -gt 0) {
                # Create the shell object
                $shell = New-Object -ComObject Shell.Application
                $folder = $shell.Namespace($directoryName)
                if ($folder -ne $null) {
                    $item = $folder.ParseName($fileName)
                    if ($item -ne $null) {
                        $verbs = $item.Verbs()
                        for ($i = 0; $i -lt $verbs.Count; $i++) {
                            $verb = $verbs.Item($i)
                            if ($verb.Name -eq $szPinToStartLocalized.ToString()) {
                                $verb.DoIt()
                                $success = $true
                                break
                            }
                        }
                    }
                }
            }
            [System.Runtime.InteropServices.Marshal]::FreeHGlobal($hShell32)
        }
        return $success
    }
    # Example usage
    PinToStart "C:\Path\To\Your\File.txt"
    

    The original code was written in C++ so you can't (without some work) use the Add-Type cmdlet to add the original code to your program as you can with C#.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.