PowerShell command to PIN apps to Windows taskbar

Dennis Stroble 1 Reputation point
2020-09-06T04:28:31.917+00:00

I'm looking for a PowerShell command or script that will pin these applications that I commonly use to the Windows taskbar at the bottom of Windows. (I have included the path to these programs):

  1. Computer management. Path: %windir%\system32\compmgmt.msc
  2. The Windows PowerShell icon. Path: %windir%\System32\WindowsPowerShell\v1.0\powershell.exe
  3. The Task Scheduler icon Path: %windir%\system32\taskschd.msc
  4. The command prompt icon. Path: %windir%\system32\cmd.exe
  5. The Internet Information Services (IIS) Manager icon. Path: %windir%\system32\inetsrv\InetMgr.exe
  6. The Notepad icon. Path: %windir%\system32\notepad.exe

This script will mostly be used in environments where I don't have access to Active Directory or Group Policy but where I can have the AD team put in a PowerShell script to run one time on several servers or where I will be running this PowerShell script whenever necessary on the individual servers and workstations that I am customizing.

Someone wrote the following script which needs just a few more modifications until it will work properly:

function New-ShortCut
{
[cmdletbinding()]
Param
(
[parameter(Mandatory)]
[ValidateScript({ Test-Path -path $_ })]
[string] $sourceExe,

    [parameter(ValueFromPipelineByPropertyName)]
    [string]$Arguments,

    [parameter(ValueFromPipelineByPropertyName)]
    [ValidateScript({
        (Test-Path -path $_) -and ( (Get-Item -path $_).PSIsContainer )
    })]
    [string]$WorkingDirectory,

    [parameter(ValueFromPipelineByPropertyName)]
    [string] $DestinationLinkName = '{0}\temp.lnk' -f  [environment]::GetFolderPath("desktop"),

    [parameter(ValueFromPipelineByPropertyName)]
    [ValidateSet('Default','Maximized','Minimized')]
    [string]$WindowStyle = 'Default',

    [parameter(ValueFromPipelineByPropertyName)]
    [ValidateScript({ Test-Path -path $_ })]
    [string]$IconPath,

    [parameter(ValueFromPipelineByPropertyName)]
    [ValidateScript({ $null -ne $IconPath })]
    [int]$IconIndexNumber,

    [parameter(ValueFromPipelineByPropertyName)]
    [string]$HotKeyString

)

$wshShell = New-Object -ComObject WScript.Shell

$WindowStyles = @{
    Default     = 1
    Maximized   = 3
    Minimized   = 7
}

$shortcut = $wshShell.CreateShortcut( $DestinationLinkName )
$shortcut.TargetPath = $sourceExe

if ($arguments)         { $shortcut.Arguments = $Arguments }
if ($WorkingDirectory)  { $shortcut.WorkingDirectory = $WorkingDirectory }
if ($WindowStyle)       { $shortcut.WindowStyle = $WindowStyles.$WindowStyle }
if ($HotKeyString)      { $shortcut.Hotkey = $HotKeyString}
if ($IconPath) {
    if ($IconIndexNumber) {
        $shortcut.IconLocation = '{0},{1}' -f $IconPath,$IconIndexNumber
    }
    else {
        $shortcut.IconLocation = $IconPath
    }
}

try
{
    $shortcut.Save()
}
catch
{
    $_.Exception.Message
}
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject($wshShell)

}

.\New-ShortCut -sourceExe 'c:\windows\System32\mmc.exe' -arguments 'compmgmt.msc' -DestinationLinkName $env:userprofile\desktop\ComputerManagement.lnk

.\New-Shortcut -sourceExe 'c:\windows\system32\windowspowershell\v1.0\powershell.exe' -DestinationLinkName $env:userprofile\desktop\powershell.lnk

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,387 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,361 Reputation points Microsoft Vendor
    2020-09-08T03:20:28.517+00:00

    Hi,

    What is your Windows edition? Just creating shortcuts may not pin the applications to the taskbar. You may use the COM object shell.application and invoke the verb "Pin to Taskbar".
    This link could be helpful
    https://stackoverflow.com/questions/31720595/pin-program-to-taskbar-using-ps-in-windows-10

    In Windows 10, you could configure the taksbar with a layout xml file.
    https://learn.microsoft.com/en-us/windows/configuration/configure-windows-10-taskbar

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.