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
        Modify existing shortcuts (links) to run application in elevated mode (Run as administrator).

            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.

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

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

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




        DefaultParametersetName = 'Path',
        SupportsShouldProcess = $true,
        PositionalBinding = $false,
        HelpURI = 'https://techstronghold.com/',
        ConfirmImpact = 'Medium'

            Mandatory = $false,
            Position = 0,
            ParameterSetName = 'Path',
            ValueFromPipelineByPropertyName = $true
        [ValidateLength(1, 255)]

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

        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


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


One response to “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 *

Active Directory Advanced function AlwaysOn Availability Groups AlwaysOn Failover Cluster Instances Building Cloud Cloud Cluster Cmdlet Database Deployment Design DFS Domain Controller DSC Fabric Failover Clustering File Server Group Policy Hardware Profile Host Hyper-V Installation Library Library Asset Library Server Network Operations Manager Orchestrator PowerShell PowerShell User Group PowerShell Workflow Security Service Manager SQL Server Storage System Center Template Time Time Synchronization Tips Virtual Machine Virtual Machine Manager VM Network VM Template Windows Server 2012 R2