cmd-Check-if-Admin-and-if-not-restart-as-Admin OneLiner

Discussion in 'Scripting' started by maelcum, Oct 2, 2017.

  1. maelcum

    maelcum MDL Novice

    Mar 19, 2011
    20
    34
    0
    #1 maelcum, Oct 2, 2017
    Last edited: Nov 4, 2017
    Hello,

    I've seen various solutions to check at some point in a (cmd-)script for admin privileges and if not, advise the user to restart the script. There are also solutions change the context from user to admin automatically.
    Allow me to throw my hat into the ring, so to say: The following one-liner has proved effective for me:

    Code:
    >nul: fltmc || powershell.exe "Start-Process cmd.exe -ArgumentList '/c \"%~f0\"' -Verb RunAs" && exit
    Everything before that line is run in whatever the context of the script was started in.
    Everything after that line is run in the contect of the administrator.
     
  2. GodHand

    GodHand MDL Senior Member

    Jul 15, 2016
    317
    281
    10
    #2 GodHand, Oct 26, 2017
    Last edited: Oct 26, 2017
    *Edit*

    Disregard.