Monday, July 20, 2020

Azure/PowerShell: Virtual Machines created from Image, Adding Applications to the Taskbar

Once an Azure virtual machine is created from an image, users can be added either local users or by adding the machine to a domain. As part of user setup, it is convenient to add applications to taskbar from a user. PowerShell is just the automation tool to handle this task.

The Taskbar is part of Windows Shell (thinking 1990s COM). It is no surprise that the code to add an application to the Taskbar requires accessing a COM object:
        $shellApplication = New-Object -ComObject shell.application
        $taskbarApplicaxtion = $shellApplication.Namespace($directory).ParseName($candidate.Name) 
        $taskbarApplicaxtion.invokeverb('taskbarpin')


The PowerShell script to add the following applications to the TaskBar is below:
  • Chrome
  • Edge
  • Notepad++
  • Visual Studio Code
  • Visual Studio
  • SQL Server Management Studio
The PowerShell script is as follows:

[string] $shellApplicationVerbTaskbarPin = 'taskbarpin'

function Add-TaskbarApplication() {
    param (
        [Parameter(Mandatory = $true)]
        [string] $applicationExecutable,
        [Parameter(Mandatory = $false)]
        [string] $applicationCandidatePath = ${env:ProgramFiles(x86)}
    )

    $candidates = Get-ChildItem `
        -Path $applicationCandidatePath `
        -Filter $applicationExecutable `
        -Recurse `
        -ErrorAction SilentlyContinue `
        -Force
    if ($null -eq $candidates) {
        # Application not found
        return 
    }

    foreach ($candidate in $candidates) {
        [string] $directory = Split-Path -Path $candidate.FullName

        $shellApplication = New-Object -ComObject shell.application
        $taskbarApplicaxtion = $shellApplication.Namespace($directory).ParseName($candidate.Name)
        if ($null -eq $taskbarApplicaxtion) {
            continue
        }
    
        $taskbarApplicaxtion.invokeverb($shellApplicationVerbTaskbarPin)
        break # certain files like msedge.exe are in multiple locations so only one pinning
    }
}

Add-TaskbarApplication 'chrome.exe' 
Add-TaskbarApplication 'msedge.exe'
Add-TaskbarApplication 'notepad++.exe'
# Visual Studio Code
Add-TaskbarApplication 'code.exe' $env:LOCALAPPDATA
# Visual Studio
Add-TaskbarApplication 'devenv.exe'
# SQL Server Management Studio
Add-TaskbarApplication 'ssms.exe'



No comments :

Post a Comment