PowerShell advanced function (cmdlet) to modify existing shortcuts (links, LNK files) to start app in elevated mode (Run as Administrator)

I needed to modify tens of existing shortcuts to run referenced applications in elevated mode. It is possible to do it in properties of the shortcut (Advanced – [x] Run as administrator) but I prefer PowerShell (actually .NET Framework) to do bulk operations. I wrote this function for you.

Function Set-RvShortcutToRunAsAdministrator
{
    <#
	.SYNOPSIS
        Modify existing shortcuts (links) to run application in elevated mode (Run as administrator).

    .DESCRIPTION
        Developer
            Developer: Rudolf Vesely, http://rudolfvesely.com/
            Copyright (c) Rudolf Vesely. All rights reserved
            License: Free for private use only

            "RV" are initials of the developer's name Rudolf Vesely and distingue names of Rudolf Vesely's cmdlets from the other cmdlets.

        Description
            Modify existing shortcuts (links) to start application in elevated mode (Run as administrator).

    .EXAMPLE
        'Single file: Modify link to Run as administrator (elevate)'
        Set-RvShortcutToRunAsAdministrator -Path 'C:\Temporary\my link.lnk' -Verbose

    .EXAMPLE
        'All files: Modify link to Run as administrator (elevate)'
        Get-ChildItem -Path 'C:\Temporary\test' -Filter *.lnk | Set-RvShortcutToRunAsAdministrator -Verbose

    .INPUTS

    .OUTPUTS

    .LINK
        https://techstronghold.com/
    #>

    [CmdletBinding(
        DefaultParametersetName = 'Path',
        SupportsShouldProcess = $true,
        PositionalBinding = $false,
        HelpURI = 'https://techstronghold.com/',
        ConfirmImpact = 'Medium'
    )]

    Param
    (
        [Parameter(
            Mandatory = $false,
            Position = 0,
            ParameterSetName = 'Path',
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateLength(1, 255)]
        [Alias('FullName')]
        [string[]]$Path
    )

    Begin
    {
        $ErrorActionPreference = 'Stop'
        Set-PSDebug -Strict
        Set-StrictMode -Version Latest
    }

    Process
    {
        foreach ($pathItem in $path)
        {
            if (Test-Path -Path $pathItem -PathType Leaf)
            {
                $fileItem = Get-Item -Path $pathItem

                if ($fileItem.Extension -eq '.lnk')
                {
                    Write-Verbose -Message ('        - Path: {0}' -f $fileItem.FullName)

                    $newFilePath = Join-Path `
                        -Path $fileItem.Directory.FullName `
                        -ChildPath ('{0} - Copy {1}{2}' -f $fileItem.BaseName, (Get-Random -Minimum 9999 -Maximum 99999999), $fileItem.Extension)

                    $writer = New-Object -TypeName System.IO.FileStream `
                        -ArgumentList $newFilePath, ([System.IO.FileMode]::Create)

                    $reader = $fileItem.OpenRead()

                    while ($reader.Position -lt $reader.Length)
                    {
                        $byte = $reader.ReadByte()
                        if ($reader.Position -eq 22)
                        {
                            $byte = 34
                        }
                        $writer.WriteByte($byte)
                    }

                    $reader.Close()
                    $writer.Close()

                    $fileItem | Remove-Item
                    Rename-Item -Path $newFilePath -NewName (Split-Path -Path $pathItem -Leaf)
                }
                else
                {
                    Write-Warning -Message ('The file has to have LNK extension: {0}' -f $pathItem)
                }
            }
            else
            {
                Write-Warning -Message ('The file does not exists: {0}' -f $pathItem)
            }
        }
    }

    End
    {
    }
}

One Comment on “PowerShell advanced function (cmdlet) to modify existing shortcuts (links, LNK files) to start app in elevated mode (Run as Administrator)

  1. Is it be possible to filter the links by "target path" , or "target folder" ? Exemple, when I add this : Get-ChildItem -Path ‘C:\test\’ -recurse | Where-Object {$_.Name -like "*lnk*" -and $_.Name -like "*testshortcut*"} | Set-RvShortcutToRunAsAdministrator -Verbose It will change all shortcuts .lnk containing the name "testshortcut" in folder (and subfolders of) C:/test . But I would like to change only the shortcuts pointing to, let’s say, "C:\Target " folder.

Leave a Reply

Your email address will not be published. Required fields are marked *