Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A single-line group of commands to add a user with password, and add to a security group. I got too much time on my hands...
rem UBER COMMAND!! (v2)
rem One-liner (technically) - prompts for User, Pass, SecGroup. Includes validation, pre-checks for UAC elevated privileges, checks for user pre-existence and group existence before running, requires a User and Group.
cmd /v /c "cls&(net file >nul 2>&1 && ((set /p "u=Enter username: "&&(net user !u! >nul 2>&1&&echo Error: user !u! already exists||(set /p "p=Password: "& set /p "g=Security Group: "&&(net localgroup !g! >nul 2>&1&& (net user !u! !p! /add>nul&&net localgroup !g! /add !u!>nul&&echo SUCCESS: Added new user !u! to group !g!&net user >nul 2>&1)|| (echo Error: specified group does not exist & net user >nul 2>&1))||(echo Error: you must specify a group)))||echo Error: user must not be blank)) || echo Error: you should run as admin)&echo.&echo.&pause"
rem Not-so-UBER command (v1)
rem One-liner, set the User, Pass, Group in the beginning of the command. No prompts, no validation. Lame.
cmd /v /c "set u=myUser&set p=password1&set g=Administrators&cls&net user !u! !p! /add>nul 2>&1&&echo User !u! added&&net localgroup !g! /add !u!>nul 2>&1&&echo Added !u! to !g!&echo.&echo.&pause"
rem (WARNING!!) Same as previous, except this one will delete the user, if it does exist - then (re)add it (WARNING!!)
cmd /v /c "set u=myUser&set p=password1&set g=Administrators&cls&(net user !u! /delete>nul 2>&1&&echo User !u! deleted||echo User !u! not found)&net user !u! !p! /add>nul 2>&1&&echo User !u! added&&net localgroup !g! /add !u!>nul 2>&1&&echo Added !u! to !g!&echo.&echo.&pause"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment