Run Visual Studio always as administrator in Windows 10

If you plan on using Visual Studio with IIS it will need to run as an administrator. This is so Visual Studio can interact with IIS to set up the necessary virtual directories. If you don't run Visual Studio as an administrator then it won't even load the ASP.NET website.

In Windows 7 (and the latest version of Windows 10) this was easy, right click on a program and set the compatibility mode so it always ran as an Admin.

In later versions of Windows that stopped working (until the recent install of Windows 10). In the past, I created a shortcut and set it to always launch Visual Studio as an administrator. But that meant I always had to open up Visual Studio first and then load the project or solution. If I double-clicked on a solution in windows explorer I would have to wait till the solution loaded, closed Visual Studio, opened it back up as an administrator and then load the solution from the recent solution list.

Now I have to remember to setup Visual Studio to run as an Administrator, which is just another manual step I have to do. Because of my current role, I often have to install multiple versions of Visual Studio on my machine. Through Google and Stackoverflow I found out how to set that automatically after running my automated install scripts. Below is a script that will find all instances of Visual Studio and set them to run as administrator.

Please note: this increases the attack area on your computer since Visual Studio will always run as an administrator. Make sure you keep up to date with your anti-virus and patches to help mitigate this risk.

Write-Host "Finding all installed versions of Visual Studio"

$visualStudioInstances = Get-ChildItem -Path "C:\Program Files (x86)\" -Filter devenv.exe -Recurse -ErrorAction SilentlyContinue -Force | %{$_.FullName}

Write-Host "Found the following Visual Studio $visualStudioInstances instances"  

$pathToCheck = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
$pathExists = Test-Path -Path $pathToCheck

if ($pathExists -eq $false)  
{
    Write-Host "The registry path $pathToCheck does not exist, creating now"
    New-Item -Path $pathToCheck -Force
}

$compabilityItems = Get-ItemProperty $pathToCheck

foreach ($visualStudioPath in $visualStudioInstances)  
{
    if ($compabilityItems.$visualStudioPath -eq $null)
    {
        Write-Host "Visual Studio at $visualStudioPath is not set to run as Admin, doing that now" 
        New-ItemProperty -Path $pathToCheck -Name $visualStudioPath -Value "^ RUNASADMIN" -PropertyType String -Force
    }
    else{
        Write-Host "Visual Studio at $visualStudioPath is already set to run as Admin"
    }
}
Author image
About Bob Walker
Omaha, NE
Founder of CodeAperture.io. Principal Software Architect in Omaha, Nebraska. Friend of Redgate. Working as a Full Stack Developer since 2004.