

This vbsscript, compatable with VBA, runs a verb from right click menu on a file. Press when ready"" & taskkill /f /im ""cmd.exe"") || echo. Run "cmd /c tasklist |find ""cmd.exe"" >nul & (set /p""= Holding on VBA till you close admin windows. 'we need some 'waitonrun', here a simple confirmation window Run "cmd /c net session >nul 2>&1 & if ERRORLEVEL 1 ( Powershell -Command ""& "" )", 1, True Psargs = msg1 & " & dir " & output & " /q & " & msg2 & " & " & mycmd & " & " & msg3 & " & pause" 'there are some warnings you can see when using -noexit after powershell cmd but it doesn't seems to hurt 'from the last cmd point of view it will be the same cmd line, a succession of cmd arg1 & arg2 & arg3, the 'encapsulation' between \"""" is a bit more tricky 'add an msdos '&' between msdos args and cut the vba string with a vba '&' where you want to insert vba variables Msg3 = "echo.& echo.""Done! now press "" & echo." Msg2 = "echo.& echo.""- applying cmd"" & echo.& echo. Msg1 = "echo.& echo.""- listing files with ownership"" & echo." 'trying to lighten a bit the expression and the cmd promptĭim msg1 As String, msg2 As String, msg3 As String 'note: first run of powershell may take some timeĭim psargsList As String, psargs As String 'exact same versions but different syntax, the first is shorter, the second uses -ArgumentList argument of powershell that can be usefull in other cases Sub acmd()ĭim output As String: output = Environ("userprofile") & "\Desktop\test.txt" ' a fileĭim mycmd As String: mycmd = "icacls " & output & " /grant %username%:F " 'an msdos cmd to run as admin Here an example to take back ownership of a file using icacls.

/004_how-to-open-an-elevated-command-prompt-2618088-5bf5e9e346e0fb00517d716a.jpg)
The 'waitonrun' prompt allow to pause vba until you finished. Other commands instead of sending a bunch of arguments. Last cmd prompt (admin mode) will be granted if you want to type Other possible use: use psargsList="echo." in psmeth 2, access to To open a cmd window in admin mode and then follow the instructions. Parametrized with vba variables, and run/compile. How it works: Enter your cmd arguments in mycmd variable, it can be Need to wait or verify anything, they can be 'released'. another cmd prompt to serve as 'waitonrun'īut also to check that no terrible thing happened. The problem: VBA wont wait for the last opened cmd window The trick: instead of running an external batch file or else, allĬommand are send in assembly line using dos & operator. That opens a cmd prompt in admin mode that runs some cmd lineĪrguments. What it does: run wsshl from vba that opens a cmd prompt that testĬurrent user rights, if not admin then it opens a powershell window Well, I may be late! say it is for the record :) Trying to answer the same question, the other topics I've read do not mention vba so I propose here a way to do it.
