Power shell and why its 4000+ commands makes life easier for administrators

For any new and aspiring IT Administrator, Powershell can be daunting and often times scary for the uninitiated. And they would be absolutely right. Powershell can be confusing, but it can also be a wonderful tool that can save you tons of time and possibly a huge headache. For most IT administrators, Powershell can be a love-hate relationship.

So what exactly is Powershell? Powershell is a powerful command-line, shell-based scripting language that enables administrators to perform their administrative duties in the command line. While end users might be more comfortable with a graphical user interface (GUI), Powershell is the result of IT administrators requesting a more efficient way to do things than they could with a GUI.

Imagine you were given a CSV (Comma Separated Values) file from your boss with the user information of 1000 employees to add into your company Active Directory database. You can add them one at a time through the GUI, but that would be incredibly tedious, time consuming, and by the end of it all, you might end up looking a bit like my niece on a Saturday morning. Not exactly a fun time.

Zero funs were had.

So, let’s take a look at how we can utilize Powershell to effectively turn this tedious process into a few easy steps and a few command line commands.

Preparing the CSV file

First, let’s prepare the CSV file to be imported into our Active Directory database. You might have to format it so that all the fields contained in that CSV file are fields that are importable with Powershell (Click here for a list of all importable fields).

For simplicity’s sake, let’s just work with a simple CSV file with only 5 users with the fields SamAccountName, Name, Surname, DisplayName, and State. Here is what the CSV file would look like when opened with notepad with the given fields:

The first line contains the fields that will correspond with Active Directory

Once we have the CSV prepared, we can enter the following command in Powershell to test our CSV file to make sure all our fields are correctly entered:

Import-Csv .\userimport1.csv

If everything turns out well, we should see a list of all the users that we have in our CSV displayed correctly with the corresponding attributes, then we know that our CSV is ready to be imported into Active Directory.

Importing the CSV file with Powershell

Now that we have prepared our CSV file properly, we are ready to import our CSV file into our database with Powershell. To do that, we will use the following Powershell command and hit Enter:

Import-csv .\userimport1.csv | New-ADuser -enabled $true -AccountPassword (ConvertTo-SecureString Pa$$w0rd -asplaintext -Force)

To verify that all our users are added, we can open “Active Directory Users and Computers” and look in the default Users container and we should find all our users found in our csv.

Let’s dissect the command and talk a bit about the different parameters:

“Import-csv .\userimport1.csv | New-ADuser -enabled $true”
This part of the command will indicate where the source of the data is located and using the |(Pipe) will direct the data to the following Powershell command, New-ADuser, which will then create the new users using the data in the CSV. Since new users created in Active directory will be disabled by default, we want to enable the newly created users with the “enabled $true” parameter.

“-AccountPassword (ConvertTo-SecureString Pa$$w0rd -asplaintext -Force)”
Since creating new user accounts in Active Directory without a password is a security risk, we must set a password for the new users. To do this, we use the “-AccountPassword” parameter to set the password to “Pa$$w0rd”. The “ConvertTo-SecureString” will convert our plaintext password into a secure string.

Revel in all your Productivity

We can do the same exact thing to add 100 users or 10,000 users. This saves a tremendous amount of time for Admins over doing it through the GUI, and this is only a small example of all the things that can be done more efficiently with over 4000+ Powershell commands.

Show your support

Clapping shows how much you appreciated Hui W. Wu’s story.