Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poweshell script to create shortcut with properties

I am follwing this msdn document. I want to create a shortcut with two properties AppUserModelId and ToastActivatorCLSID. How can I achieve this with powershell.

Basically I want to convert the following code and create shortcut via powershell script.

_Use_decl_annotations_
HRESULT DesktopToastsApp::InstallShortcut(PCWSTR shortcutPath, PCWSTR exePath)
{
    ComPtr<IShellLink> shellLink;
    HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink));
    if (SUCCEEDED(hr))
    {
        hr = shellLink->SetPath(exePath);
        if (SUCCEEDED(hr))
        {
            ComPtr<IPropertyStore> propertyStore;

            hr = shellLink.As(&propertyStore);
            if (SUCCEEDED(hr))
            {
                PROPVARIANT propVar;
                propVar.vt = VT_LPWSTR;
                propVar.pwszVal = const_cast<PWSTR>(AppId); // for _In_ scenarios, we don't need a copy
                hr = propertyStore->SetValue(PKEY_AppUserModel_ID, propVar);
                if (SUCCEEDED(hr))
                {
                    propVar.vt = VT_CLSID;
                    propVar.puuid = const_cast<CLSID*>(&__uuidof(NotificationActivator));
                    hr = propertyStore->SetValue(PKEY_AppUserModel_ToastActivatorCLSID, propVar);
                    if (SUCCEEDED(hr))
                    {
                        hr = propertyStore->Commit();
                        if (SUCCEEDED(hr))
                        {
                            ComPtr<IPersistFile> persistFile;
                            hr = shellLink.As(&persistFile);
                            if (SUCCEEDED(hr))
                            {
                                hr = persistFile->Save(shortcutPath, TRUE);
                            }
                        }
                    }
                }
            }
        }
    }
    return hr;
}

I wrote a simple script to create a shortcut however not sure how can I stamp the said two props

function set-shortcut {

param ( [string]$SourceLnk, [string]$DestinationPath )

    $WshShell = New-Object -comObject WScript.Shell

    $Shortcut = $WshShell.CreateShortcut($SourceLnk)

    $Shortcut.TargetPath = $DestinationPath

    $Shortcut.Save()

    }
like image 357
Vallabh Patade Avatar asked Mar 21 '26 08:03

Vallabh Patade


1 Answers

Why not just use the pre-built modules/scripts from the MS powershellgallery.com to determine if they solve your use case vs rewrite from scratch. Unless this is a learning effort. Even with that, looking at the underlying code of the below may help.

Find-Module -Name '*shortcut*'

# Results
<#
Version   Name                        Repository Description
-------   ----                        ---------- -----------
2.2.0     DSCR_Shortcut               PSGallery  PowerShell DSC Resource to create shortcut file.
...
1.0.6     PSShortcut                  PSGallery  This module eases working with Windows shortcuts (LNK and URL) files.
...
#>


Find-Script -Name '*Shortcut*'
# Results
<#
Version Name                       Repository Description   
------- ----                       ---------- -----------   
...                        
1.0.0.0 New-DeskTopShortCut        PSGallery  Create a desktop shortcut
#>
like image 82
postanote Avatar answered Mar 22 '26 23:03

postanote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!