How to add run MSI as administrator to context menu in Windows 8 (PowerShell)

Introduction

This script is to add Run MSI as Administrator to context menu in Windows 8.

Scenarios

Some MSI files need to run with administrator privilege. However there are no Run MSI as Administrator options in the Windows system. This script is to add this right click option for MSI files.

Script

Step 1: Right click the script and select Run with Powershell.

Step 2: Then after the script finishes execution, you will see the result.

Here are some code snippets for your reference:

PowerShell
Edit|Remove
#Create HKCR provider 
New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR | Out-Null 
$regpath1 = "HKCR:\Msi.Package\shell\runas" 
$regpath2 =  "HKCR:\Msi.Package\shell\runas\command" 
#Check if item has been already added 
if(Test-path -Path $regpath2 ) 
{ 
    $choice = Read-Host "You have add  run msi file with adminsitrator to context menu,do you want to remove it(Y or N)?" 
    if($choice -eq "y") 
    { 
        #Remove the item  
        Remove-Item  -Path $regpath1 -Recurse -Confirm:$False -ErrorAction SilentlyContinue 
        Write-Host â€œRemove item successfully.” 
    } 
    Elseif($choice -eq "n") 
    { 
        Write-Host "Operation cancelled by user." 
    } 
    Else 
    { 
        Write-Warning "Invalid input value."    
    } 
} 
Else 
{ 
    #Create registry value 
    New-Item  -Path $regpath1  -ErrorAction SilentlyContinue | Out-Null 
    Set-ItemProperty -Path  $regpath1  -Name "(Default)" -Value  "Install &as Administrator" -ErrorAction SilentlyContinue | Out-Null 
    New-Item -Path $regpath2 -ErrorAction SilentlyContinue | Out-Null 
    Set-ItemProperty -Path "HKCR:\Msi.Package\shell\runas\command" -Name "(Default)" -Value  "msiexec /i \`"%1\`"" -ErrorAction SilentlyContinue | Out-Null 
    Write-Host "Add run msi with administrator successfully." 
  
} 
Remove-PSDrive -Name HKCR
Prerequisite

Windows 8 or higher versionAll-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.