Thursday 15 October 2020

IsAdmin.exe Returns Yes to standard out and a errorlevel of 0 if user is elevated else returns No and errorlevel of 1

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
REM IsAdmin.bat
REM This file compiles IsAdmin.vb to IsAdmin.exe
REM IsAdmin.exe Says if the user is running elevated
REM To Use
Rem          IsAdmin 
REM         Return Yes and Errorlevel = 0 if user is admin and elevated else returns No and Errorlevel = 1
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\IsAdmin.exe" "%~dp0\IsAdmin.vb" 
pause

'IsAdmin.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication 
 
	Public Declare Function IsUserAnAdmin Lib "Shell32" () As Boolean

Sub Main()
	If IsUserAnAdmin() = True then
		Console.writeline("Yes")
		Environment.ExitCode = 0

	Else
		Console.writeline("No")
		Environment.ExitCode = 1
	End If
End Sub
End Module