This project is read-only.

Set-PinnedApplication -Action "PintoTaskBar" for all users

Topics: Archive - General
Dec 10, 2014 at 11:50 AM
Hi,

I am configuring an Office 2013 installation package, and am having issues with pinning shortcuts to the taskbar for the currently logged in user.

I can't get it to work via the OCT shortcuts route so have been trying from within the Deploy-Application.ps1, running this command:
Set-PinnedApplication -Action "PintoTaskBar" -FilePath "$envProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2013\Outlook 2013.lnk" 
How do I run this line in the currently logged in users context rather than the admin user I'm running the script as please ? Its currently creating the shortcut only in the admin users taskbar.

Many Thanks.
Developer
Dec 11, 2014 at 9:34 AM
Well you can use an active setup entry to have it created for users upon first log in. Look at the latest beta under the "Source Code" tab of this site for the Set-ActiveSetup function.
Dec 11, 2014 at 3:43 PM
Hi

Thanks for the reply. I'll try that out when I get a chance and report back.
Dec 22, 2014 at 7:28 PM
Edited Jan 5, 2015 at 7:04 PM
I wasn't aware of the Active Setup option when I did my deployment. Instead, I wrote some code in the Post-Install section to parse the User profiles, check for pinned Taskbar items from 2010, create a new reg key to keep track, then add a new entry in each user's RunOnce to execute a script on their next login. I know it likely isn't the cleanest method, but it has worked consistently for me.

EDIT: I ended up looking into ActiveSetup, and it provided a much cleaner approach. I've modified the entries below to show what I currently run. You will need to add the Set-ActiveSetup function code to your toolkit extensions file

Post install code:
        # Find all all Office 2010 Links and remove them
        Write-Log "Finding all old TaskBar Links, writing a registry entry to keep track, and removing the old files"

        Copy-File $dirSupportFiles\ReplaceOldLinks.ps1 -Destination "C:\users\Public"
        
        $users = Get-UserProfiles | Where-Object{$_.SID -ine "S-1-5-21-Default-User"} 
        
        foreach ($u in $users)
        {
            If(Test-Path -Path "Registry::HKEY_USERS\$($u.SID)")
            {
                $links = Get-ChildItem -Path "$($u.ProfilePath)\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\" -Filter "*2010.lnk" -ErrorAction SilentlyContinue
                $regBase = "Registry::HKEY_USERS\$($u.SID)\Software\Microsoft\Office\15.0\Links"

                foreach($l in $links){
                    switch -wildcard ($l.Name) 
                        { 
                            "*Outlook*" {Set-RegistryKey -Key "$regBase\Outlook"; write-log "Adding to registry: $regBase\Outlook" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                            "*Access*" {Set-RegistryKey -Key "$regBase\Access"; write-log "Adding to registry: $regBase\Access" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                            "*Excel*" {Set-RegistryKey -Key "$regBase\Excel"; write-log "Adding to registry: $regBase\Excel" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                            "*InfoPath*" {Set-RegistryKey -Key "$regBase\InfoPath"; write-log "Adding to registry: $regBase\InfoPath" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                            "*OneNote*" {Set-RegistryKey -Key "$regBase\OneNote"; write-log "Adding to registry: $regBase\OneNote" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                            "*PowerPoint*" {Set-RegistryKey -Key "$regBase\PowerPoint"; write-log "Adding to registry: $regBase\PowerPoint" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                            "*Publisher*" {Set-RegistryKey -Key "$regBase\Publisher"; write-log "Adding to registry: $regBase\Publisher" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                            "*Word*" {Set-RegistryKey -Key "$regBase\Word"; write-log "Adding to registry: $regBase\Word" -Source "ReplaceOldLinks";Remove-Item $l.FullName -Force -ErrorAction SilentlyContinue} 
                        }
                }
            }
        }
        Set-ActiveSetup -StubExePath "$($PSHOME)\powershell.exe" -Arguments '-noninteractive -noprofile -executionpolicy bypass -file "c:\users\public\ReplaceOldLinks.ps1" -windowstyle Minimized' -Description 'Office 2013 User Configuration' -Key 'Office2013UserConfig' -Locale 'en'
ReplaceOldOfficeLinks.ps1
if(Test-Path -Path HKCU:\SOFTWARE\Microsoft\Office\15.0\Links)
{
    $oldlinks = (Get-ChildItem -Path HKCU:\SOFTWARE\Microsoft\Office\15.0\Links)

    $linkbase = "$($env:ALLUSERSPROFILE)\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2013"

    foreach($link in $oldlinks){

    Switch -wildcard ($link.PSChildName)
    {
        "*Access*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\Access 2013.lnk" -ErrorAction SilentlyContinue }
        "*Excel*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\Excel 2013.lnk" -ErrorAction SilentlyContinue }
        "*InfoPath*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\InfoPath Designer 2013.lnk" -ErrorAction SilentlyContinue }
        "*OneNote*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\OneNote 2013.lnk" -ErrorAction SilentlyContinue }
        "*Outlook*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\Outlook 2013.lnk" -ErrorAction SilentlyContinue }
        "*PowerPoint*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\PowerPoint 2013.lnk" -ErrorAction SilentlyContinue }
        "*Publisher*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\Publisher 2013.lnk" -ErrorAction SilentlyContinue }
        "*Project*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\Project 2013.lnk" -ErrorAction SilentlyContinue }
        "*Word*"{ Set-PinnedApplication -Action PinToTaskbar -FilePath "$linkbase\Word 2013.lnk" -ErrorAction SilentlyContinue }
     }
    }
    Remove-Item -Path HKCU:\SOFTWARE\Microsoft\Office\15.0\Links -Force -ErrorAction SilentlyContinue -Recurse
}

function Set-PinnedApplication 
{ 
<#  
.SYNOPSIS  
This function are used to pin and unpin programs from the taskbar and Start-menu in Windows 7 and Windows Server 2008 R2 
.DESCRIPTION  
The function have to parameteres which are mandatory: 
Action: PinToTaskbar, PinToStartMenu, UnPinFromTaskbar, UnPinFromStartMenu 
FilePath: The path to the program to perform the action on 
.EXAMPLE 
Set-PinnedApplication -Action PinToTaskbar -FilePath "C:\WINDOWS\system32\notepad.exe" 
.EXAMPLE 
Set-PinnedApplication -Action UnPinFromTaskbar -FilePath "C:\WINDOWS\system32\notepad.exe" 
.EXAMPLE 
Set-PinnedApplication -Action PinToStartMenu -FilePath "C:\WINDOWS\system32\notepad.exe" 
.EXAMPLE 
Set-PinnedApplication -Action UnPinFromStartMenu -FilePath "C:\WINDOWS\system32\notepad.exe" 
#>  
       [CmdletBinding()] 
       param( 
      [Parameter(Mandatory=$true)][string]$Action,  
      [Parameter(Mandatory=$true)][string]$FilePath 
       ) 
       if(-not (test-path $FilePath)) {  
           throw "FilePath does not exist."   
    } 
    
       function InvokeVerb { 
           param([string]$FilePath,$verb) 
        $verb = $verb.Replace("&","") 
        $path= split-path $FilePath 
        $shell=new-object -com "Shell.Application"  
        $folder=$shell.Namespace($path)    
        $item = $folder.Parsename((split-path $FilePath -leaf)) 
        $itemVerb = $item.Verbs() | ? {$_.Name.Replace("&","") -eq $verb} 
        if($itemVerb -eq $null){ 
            throw "Verb $verb not found."             
        } else { 
            $itemVerb.DoIt() 
        } 
            
       } 
    function GetVerb { 
        param([int]$verbId) 
        try { 
            $t = [type]"CosmosKey.Util.MuiHelper" 
        } catch { 
            $def = [Text.StringBuilder]"" 
            [void]$def.AppendLine('[DllImport("user32.dll")]') 
            [void]$def.AppendLine('public static extern int LoadString(IntPtr h,uint id, System.Text.StringBuilder sb,int maxBuffer);') 
            [void]$def.AppendLine('[DllImport("kernel32.dll")]') 
            [void]$def.AppendLine('public static extern IntPtr LoadLibrary(string s);') 
            add-type -MemberDefinition $def.ToString() -name MuiHelper -namespace CosmosKey.Util             
        } 
        if($global:CosmosKey_Utils_MuiHelper_Shell32 -eq $null){         
            $global:CosmosKey_Utils_MuiHelper_Shell32 = [CosmosKey.Util.MuiHelper]::LoadLibrary("shell32.dll") 
        } 
        $maxVerbLength=255 
        $verbBuilder = new-object Text.StringBuilder "",$maxVerbLength 
        [void][CosmosKey.Util.MuiHelper]::LoadString($CosmosKey_Utils_MuiHelper_Shell32,$verbId,$verbBuilder,$maxVerbLength) 
        return $verbBuilder.ToString() 
    } 
 
    $verbs = @{  
        "PintoStartMenu"=5381 
        "UnpinfromStartMenu"=5382 
        "PintoTaskbar"=5386 
        "UnpinfromTaskbar"=5387 
    } 
        
    if($verbs.$Action -eq $null){ 
           Throw "Action $action not supported`nSupported actions are:`n`tPintoStartMenu`n`tUnpinfromStartMenu`n`tPintoTaskbar`n`tUnpinfromTaskbar" 
    } 
    InvokeVerb -FilePath $FilePath -Verb $(GetVerb -VerbId $verbs.$action) 
}