Function New-PinnedItem {
            Adds a pinned item to the StartMenu/Taskbar

            Adds a pinned item to the StartMenu/Taskbar

        .PARAMETER TargetPath
            Full path to the item that will be pinned

        .PARAMETER Type
            Determine where item will be pinned to.

            Acceptable values:

        .PARAMETER Argument
            Additional arguments that will be supplied to create shortcut that will
            be used to pin to Taskbar or StartMenu

        .PARAMETER IconLocation
            Location to icon file path.

        .PARAMETER ShortCutPath
            Name and place where shortcut will be created prior to pinning it to
            Taskbar or StartMenu.

        .PARAMETER WorkingDirectory
            The directory to start the process in.

            Name: New-PinnedItem
            Author: Boe Prox
            Version History
                1.0 //Boe Prox - 03 June 2015
                    - Initial Build

            New-PinnedItem -Type StartMenu -TargetPath "C:\windbg.exe"

            Pins Windbg to the StartMenu

            New-PinnedItem -TargetPath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -Type TaskBar

            Pins Internet Explorer to the TaskBar

            $TargetPath = 'PowerShell.exe'
            $ShortCutPath = 'WinDbg.lnk'
            $Argument = "-ExecutionPolicy Bypass -NoProfile -NoLogo -Command `"& 'C:\users\proxb\desktop\Windbg.exe'`""
            $Icon = 'C:\users\proxb\desktop\Windbg.exe'
            New-PinnedItem -TargetPath $TargetPath -ShortCutPath $ShortcutPath -Argument $Argument -Type TaskBar -IconLocation $Icon

            Pins an application that normally couldn't be pinned to the Taskbar by defining a custom
            shortcut that is then pinned to the Taskbar.

    Param (
            If ($_ -match '\.lnk|\.url') {
            } Else {
                Throw "The extension must end in .lnk or .url!"


    Write-Verbose "Creating Shell ComObject"
    $Shell = New-Object -ComObject Shell.Application

    If ($PSCmdlet.ParameterSetName -eq 'AltShortcut') {
        $File = $TargetPath -replace '.*\\(.*)','$1'        
        $WShell = New-Object -ComObject WScript.Shell
        If ($PSBoundParameters.ContainsKey('ShortcutPath')) {
            Try {
                $ShortcutPath = Convert-Path $ShortcutPath -ErrorAction Stop
            } Catch {
                $ShortcutPath = Join-Path $PWD $ShortcutPath
            $Shortcut = $WShell.CreateShortcut($ShortcutPath)
        } Else {
            $Link = $File -replace '^(.*)?\..*','$1.lnk'
            $Shortcut = $WShell.CreateShortcut("$($PWD)\$Link")
        Write-Verbose "ShortcutPath: $($Shortcut.fullname)"
        If ($PSBoundParameters.ContainsKey('IconLocation')) {
            $Shortcut.IconLocation = $IconLocation
        If ($PSBoundParameters.ContainsKey('Argument')) {
            $Shortcut.Arguments = $Argument
        If ($PSBoundParameters.ContainsKey('WorkingDirectory')) {
            $Shortcut.WorkingDirectory = $WorkingDirectory
        $Shortcut.TargetPath = $TargetPath
        $TargetPath = $Shortcut.Fullname

    If ($TargetPath -match '^(?<Path>.*\\)(?<File>.*)$') {
        $Path = $Matches.Path
        $File = $Matches.File
        Write-Verbose "Path: $($Path) -- File: $($File)"
        $NameSpace = $Shell.NameSpace($Path)
        $NameSpaceFile = $NameSpace.ParseName($File)
        Switch ($Type) {
            'Taskbar' {
                $_Verb = ConvertToVerb -Action PintoTaskbar
                $Verb = $NameSpaceFile.Verbs() | Where {
                    $_.Name -eq $_Verb
            'StartMenu' {
                $_Verb = ConvertToVerb -Action PintoStartMenu
                $Verb = $NameSpaceFile.Verbs() | Where {
                    $_.Name -eq $_Verb
        If ($Verb) {
        } Else {
            Write-Warning "Unable to perform action: Pin $($File) to $($Type)!"
    } Else {
        Write-Warning "Unable to parse File and Path from provided Fullname: $($TargetPath)!"

    Write-Verbose 'Cleanup ComObject'
    If ($PSCmdlet.ParameterSetName -eq 'AltShortcut') {
    Remove-Variable Shell -ErrorAction SilentlyContinue
    Remove-Variable WShell -ErrorAction SilentlyContinue