Computer Management bat files - Part 1: Users


So my game plan with this is a series of blogs on how to use bat files to create a bigger computer management script that could be used for hardening your system. I will be taking this by sections as a lot will go into this. The final article will be adding it all together to create one large script.

In today's article we are going over how to manage users with a set of simple batch files.

User Management Scripts

The first part of our scripting is to create scripts for managing user accounts. We will create individual scripts for viewing, adding, enabling, disabling, user accounts as well as changing their passwords.

How to list users

To list users is a simple command
Net User
This will list all the users on your computer as shown in the picture below.


View users account Script

Now that we know the users names lets write a script that will allow us to view their user details. we want this script though to not just list a single user but be one we can continue to use without restarting it. We are going to create variables in this script and then when done set it to go back to the beginning of the script.

     @Echo off



     :ViewUser

       setlocal EnableDelayedExpansion

       echo  Type Below Requirements:

       echo.

       :username

        set /p usr= Type Username:

        if [!usr!]==[] goto username

       net user %usr%

       pause

       goto ViewUser


This will list all the users account information and then loop to check another user, as shown in the picture below.



Add a user Script

Now that we can list all users and view their details, lets build a script using the same technique of looping to add users to the computer. We want this script to show us the current users at the same time as well, that way we don't add the same user twice.

         @Echo off

         :AddUser

               Net user

               setlocal EnableDelayedExpansion

               echo  Type Below Requirements:

               echo.

               :username

                    set /p usr= Type Username:

                    if [!usr!]==[] goto username

               :password

                    set /p pwd= Type Password:

                    if [!pwd!]==[] goto password

               echo.

               echo Your username is: !usr!

               echo Your password is: !pwd!

               pause

                   net user /add %usr% %pwd% /EXPIRES:NEVER  /PASSWORDCHG:YES /ADD

  WMIC USERACCOUNT WHERE "Name='%usr%'" SET PasswordExpires=TRUE

               pause

               goto AddUser


The ran script should look like below.



Change a users password

Now that we can create a user lets write a script to change their password.

     @Echo off



     :Password

      net user

       setlocal EnableDelayedExpansion

       echo  Type Below Requirements:

       echo.

       :username

        set /p usr= Type Username:

        if [!usr!]==[] goto username

       :password

        set /p pwd= Type Password:

        if [!pwd!]==[] goto password

       echo.

       echo Your username is: !usr!

       echo Your password is: !pwd!

       pause

       net user %usr% %pwd% /EXPIRES:NEVER  /PASSWORDCHG:YES

       pause

       goto Password

The ran script should look like below. I did add the net user command to this to list the users. The picture just doesn't show it.

Enable a user

The next script will be able to enable a user account.
     @Echo off



     :EnableUser

      net user

       setlocal EnableDelayedExpansion

       echo  Type Below Requirements:

       echo.

       :username

        set /p usr= Type Username:

        if [!usr!]==[] goto username

       net user %usr% /active:yes

       pause

       goto EnableUser

The ran script should look like below. I did add the net user command to this to list the users. The picture just doesn't show it.

Disable a user

The next script will be able to disable a user account.
     @Echo off



     :DisableUser



           net user

            setlocal EnableDelayedExpansion

       echo  Type Below Requirements:

       echo.

       :username

        set /p usr= Type Username:

        if [!usr!]==[] goto username



       net user %usr% /active:no

       pause

       goto DisableUser

The ran script should look like below.
Here we have all the scripts done. Granted there is on more for deleting user accounts, but in the IT world deleting is a no no. Next up is doing the same for Groups.

Comments

Popular posts from this blog

Star Trek: Akira-class Bridge (TNG-era)

Hapes Consortium Uniforms