0

Is there a way to get the path of an .ico of a short cut? I know how to change the shortcuts icon, but how do I find the path of the shortcut's icon file?

I am Jakoby
  • 577
  • 4
  • 19
  • 1
    take a look at the `function Get-Shortcut ` here ... >>> file - Editing shortcut (.lnk) properties with Powershell - Stack Overflow — https://stackoverflow.com/questions/484560/editing-shortcut-lnk-properties-with-powershell/21967566#21967566 <<< it returns an object with an `.IconLocation` property. if it shows `,0` the icon is in the target, otherwise the icon is referenced with a path and an index. – Lee_Dailey May 08 '22 at 13:21
  • @Lee_Dailey that is absolutely brilliant. Do you know how I can modify the Set-Shortcut function so I can make the target different websites? As it is if you don't define an actual path it uses the short cut path as a default Set-Shortcut -LinkPath example -TargetPath youtube.com will default out to C:\Users\User\Desktop\youtube.com – I am Jakoby May 08 '22 at 13:50
  • you are most welcome! [*grin*] ///// from what i can tell ... you can't have multiple values. if you want that, then you will likely need to do something like having a script that creates new shortcuts on demand. – Lee_Dailey May 08 '22 at 15:52

1 Answers1

3

You could use below function.
It handles both 'regular' shrotcut files (.lnk) as well as Internet shortcut files (.url)

function Get-ShortcutIcon {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias('FullName')]
        [string]$Path  # needs to be an absulute path
    )
    switch ([System.IO.Path]::GetExtension($Path)) {
        '.lnk'  {
            $WshShell = New-Object -ComObject WScript.Shell
            $shortcut = $WshShell.CreateShortcut($Path)
            $iconPath = $shortcut.IconLocation
            $iconInfo = if ($iconPath -match '^,(\d+)') {
                [PsCustomObject]@{ IconPath = $shortcut.TargetPath; IconIndex = [int]$matches[1] }
            }
            else {
                [PsCustomObject]@{ IconPath = $iconPath; IconIndex = 0 }
            }

            # clean up
            $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut)
            $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WshShell)
            [System.GC]::Collect()
            [System.GC]::WaitForPendingFinalizers()

            # return the icon information
            $iconInfo
        }
        '.url' {
            $content = Get-Content -Path $Path -Raw
            $iconPath  = [regex]::Match($content, '(?im)^\s*IconFile\s*=\s*(.*)').Groups[1].Value
            $iconIndex = [regex]::Match($content, '(?im)^\s*IconIndex\s*=\s*(\d+)').Groups[1].Value
            [PsCustomObject]@{ IconPath = $iconPath; IconIndex = [int]$iconIndex }
        }
        default { Write-Warning "'$Path' does not point to a '.lnk' or '.url' shortcut file.." }
    }
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • the $iconPath variable returns: ,0 is the actual location of that icon $TargetPath+$iconpath ? How do I get the full path of the icon used with the shortcut? – I am Jakoby May 08 '22 at 21:48
  • oh you can not, they are located in .jar files you need to open with winrar – I am Jakoby May 08 '22 at 21:56