Skip to content

Instantly share code, notes, and snippets.

@markgodiy
Created January 13, 2023 23:00
Show Gist options
  • Select an option

  • Save markgodiy/609aa71db03d316ccfe284ceabef5fb6 to your computer and use it in GitHub Desktop.

Select an option

Save markgodiy/609aa71db03d316ccfe284ceabef5fb6 to your computer and use it in GitHub Desktop.
Execute Powershell Script using VBA, with UAC for Security. To use this script as-is, .vbs file must be in the same folder as .ps1 file, then create shortcut to .vbs file as needed.
Function RunAsAdmin()
Dim objAPP
If WScript.Arguments.length = 0 Then
Set objAPP = CreateObject("Shell.Application")
objAPP.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" & " RunAsAdministrator",,"runas", 1
WScript.Quit
End If
End Function
RunAsAdmin()
strScriptPath = Wscript.ScriptFullName
'msgbox strScriptPath
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strScriptPath )
strScriptFolder = objFSO.GetParentFolderName(objFile)
strCmd = "powershell.exe -noexit -executionpolicy remotesigned -file """ & strScriptFolder & """\MyScript.ps1"""
'msgbox strCmd
Set shell = WScript.CreateObject("WScript.shell")
shell.Run(strCmd), 1 , True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment