pf-ShortCuts.ps1


function Add-ShortCut ($Uri, $name, $HotKey, $destination, $user) {
    begin {
        if (-not $destination) {
            $destination = ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop))
        }
        $shell = (New-Object -ComObject WScript.Shell)

        $patternList = @( 
                @{ like = '*.ps1'; app = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' }
                @{ like = 'http*//*'; app = 'C:\Program Files\Internet Explorer\iexplore.exe' }
            )
    }
    process {
        if (-not $name){
            $name = Split-Path $Uri -Leaf
        }
        $linkPath          = Join-Path $destination "$name.lnk"
        if (Test-Path $linkPath) {
            Remove-Item $linkPath -Force
        }

        $link = $shell.CreateShortcut($linkPath)
        
        function MatchPattern {
            foreach ( $pattern in $patternList ) {
                if ( $Uri -like $pattern.like ) {
                    return $pattern.app
                }
            }
        }
        
        $app = MatchPattern $Uri
        $targetPAth = $Uri
        if ( $user ) {
            $targetPAth = "runas"
            $link.Arguments = "/user:$user /savecred '$app $Uri'"
        }
        $link.TargetPath   = $targetPAth

        #$lnk.WorkingDirectory = $workingDirectory

        if ($app) {
            $link.IconLocation = "$app, 0"
        }
        if ($HotKey) {
            $link.HotKey   = if ($HotKey.length -eq 1) { "CTRL+SHIFT+ALT+$HotKey" } else { $HotKey }
        }
        $link.Save()
    }
}
function Add-ShortCut:::Example {
    Add-ShortCut -Uri "https://portal.azure.com/" -name 'Azure' -HotKey 'A'
    $IEFavoritesPath = [Environment]::GetFolderPath('Favorites','None')
    $IEFavoritesPath += "\Links"
    Add-ShortCut -Uri "https://portal.azure.com/" -name 'Azure' -destination $IEFavoritesPath
}

function Add-ISERecentErrorsFiles {
    $lines = $Error.ScriptStackTrace -split "`n"
    $records = $lines | Get-Regex_Match 'at (?<cmd>.+), (?<file>.+): line (?<line>\d+)'
    $records = $records | Where-Object file -ne '<No file>' 
    $files  = $records.file | Select-Object -Unique
    $files | Add-ISERecentFile -Clear
}
function Add-ISERecentErrorsFiles:::Example {
    try {
        throw 'Bad thing'
    }
    catch {
        Add-ISERecentErrorsFiles
    }
}

function Add-ISERecentFile ([Switch]$Clear, [Switch]$backup) {
    process {
        $path = Get-Path $_
        if (-not $path) { return }
        $userConfig = Get-ChildItem "$env:localappdata\microsoft_corporation\powershell_ise*\*\user.config"
        if ($userConfig) {
            [xml]$data = Get-Content $userConfig.FullName -Raw
            $mru = $data | select-xml '//setting[@name="MRU"]/value/ArrayOfString'
            $newMru = if ($Clear) { @( $path ) } else { @( $path ) + $mru.Node.string }
            $newValue = $newMru | ForEach-Object { "<string>$_</string>" }
            $newValue = $newValue -join "`n"
            $mru.Node.InnerXml = $newValue
            if ($backup) {
                Copy-Item $userConfig.FullName -Destination ( $userConfig.FullName + '.bak' ) -Force
            }
            #$encoding = Get-FileEncoding $userConfig.FullName
            $data.Save($userConfig.FullName)
        }
    }
}

function Add-ISERecentFile:::Example {
    ( Get-PSCallStack )[0].ScriptName | Add-ISERecentFile 
}