shortcutwindows-11

Extract all of Target field from shortcut


Some shortcuts were created by an app's installer. The Target field is greater than the limit of 259 characters for the shortcut properties GUI panel.

The two solutions here don't work.

How can I extract the entirety of the Target field without the 259 character truncation?


Solution

  • You can try this command in PowerShell console:

    ( New-Object -ComObject 'WScript.Shell' ).CreateShortcut( 'full\path\of\your\shortcut.lnk' ).Arguments

    An example of PowerShell console output, returning the arguments attribute in a simple command:

    PS D:\utilitário\notepad++> ( New-Object -ComObject 'WScript.Shell' ).CreateShortcut( 'd:\temp\newlink.lnk' ).Arguments
    -Command Write-Output -NoEnumerate 'Letra original de Almirante Negro, censurada pela ditadura militar no Brasil' 'Há muito tempo nas águas da Guanabara' 'O Dragão do Mar reapareceu' 'Na figura de um bravo marinheiro' 'A quem a história não esqueceu' 'Conhecido como Almirante Negro' 'Tinha a dignidade de um mestre-sala' 'E ao acenar pelo mar, na alegria das fragatas' 'Foi saudado no porto pelas mocinhas francesas' 'Jovens polacas e por batalhões de mulatas' 'Rubras cascatas jorravam das costas' 'dos negros pelas pontas das chibatas ' 'Inundando o coração do pessoal do porão' 'Que a exemplo do marinheiro gritava - não!' 'Glória aos piratas, às mulatas, às sereias' 'Glória à farofa, à cachaça, às baleias' 'Glória a todas as lutas inglórias' 'Que através da nossa história' 'Não esquecemos jamais' 'Salve o Almirante Negro' 'Que tem por monumento' 'As pedras pisadas do cais' 'Mas faz muito tempo…' `r ; Write-Host -Object "Aguarde 5 segundos " -NoNewline ; start-sleep -seconds 5
    
    PS D:\utilitário\notepad++> dir 'd:\temp\newlink.lnk'
    
        Diretório: D:\temp
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a----        01/05/2025     19:28           2776 newlink.lnk
    

    If the shortcut does not exist, an exception will not be thrown and empty will be returned.

    A possible solution in the script, regardless of the path being relative, returning the TargetPath and Arguments attributes, would be:

        Push-Location -LiteralPath $PSScriptRoot
        $ShortCut = '..\..\Temp\shortcut.lnk' # or any other string '.\shortcut.lnk'
        If ( Test-Path -LiteralPath $ShortCut -PathType 'Leaf' ) {
            $ShortCut = ( Resolve-Path -LiteralPath $ShortCut ).Path
            $ObjectShortCut = ( New-Object -ComObject 'WScript.Shell' ).CreateShortcut( $ShortCut )
            Write-Output -InputObject "$( $ObjectShortCut.TargetPath ) $( $ObjectShortCut.Arguments )"
        } Else {
            Write-Warning -Message "The path ""$ShortCut"" was not found."
        }
        Pop-Location
    

    In PowerShell, Push-Location and Pop-Location are cmdlets used to navigate between directories in a way similar to a stack. They allow you to save your current location and easily return to it after navigating to another place.

    Push-Location <path>: This cmdlet saves your current directory at the top of a location stack and then changes your working directory to the specified <path>. Think of it as "pushing" your current location onto a stack and going to a new place.

    Pop-Location: This cmdlet removes the last saved location from the top of the stack and returns your working directory to that location. It's like "popping" the last location you "pushed" onto the stack.

    Moving from the current directory to the location where the script is running, i.e., to the path stored in the $PSScriptRoot variable, is useful to ensure that the script works in a specific directory without losing the original context, making it easier to organize and perform tasks related to files or scripts in the same location.

    If the link is in the script folder, the command to initialize the variable could be $ShortCut = '.\shortcut.lnk'.