In this video from IT Free Training I will be looking at using PowerShell to create and
modify user accounts. In your IT career, you may have the need to create a lot of user
accounts at once. Using PowerShell is just one of the many different ways you can automate
the process of creating a lot of users at once.
PowerShell was first released in 2006 as a replacement for the command prompt. Although
a lot can be achieved using the command line, the command line essentially is designed for
batch processing. One command is run after another command. Each command interacts with
the operating system exclusively. PowerShell is designed as a task based scripting language
that is integrated with other Windows Technologies. If you have done any programming before, you
will notice some similarities between PowerShell and programming.
The first terminology you should become familiar with in PowerShell is the CmdLets. A CmdLet
is a single featured command. There designed to implement a specific function by manipulate
objects directly. This is one of the core differences between PowerShell and the command
line. PowerShell allows you to work directly with objects. In contrast to the command prompt,
you would need to pass parameters to utilities or transfer the output from one command to
anther to achieve a simpler result. CmdLets follow a standard naming convention
in the form of verb noun. Some examples are shown here. On the left side it tells you
what action it will perform, for example getting, setting or clearing. On the right hand side
it tells you the target the action will be working on. By using this convention, you can quickly
look at a CmdLet and determine what it will do.
To get a better understanding of how PowerShell works I will now open PowerShell and run a
few commands. PowerShell is found in the start menu under Programs, Accessories, Windows
PowerShell. The most noticeable difference is that the background for PowerShell is dark
blue rather than the black border found in the command prompt.
Like the command prompt you can change the current path and you can change directories
using the CD command. The CmdLet that I will look at is the CmdLet Write-Host. This will
display a message on the screen, in this case “hello world.”
You will find in PowerShell that a number of commands that can be used in the command
prompt also work in PowerShell. For example, I could achieve the same result by typing
in echo hello world at the command prompt. This may lead you to believe that a lot of
command prompt commands also exist in PowerShell. The fact is the same functionally may exist,
but the CmdLet used for that command may have changed. Let me explain why.
If I run the command alias echo, this will show that there is an alias set up for that
command. An alias is another name that PowerShell can refer to the CmdLet. In this case, when
echo is used it will instead run the CmdLet Write-Output. This makes it easier when making
the change from the command prompt to PowerShell as many CmdLets have alias to command prompt
style commands. Common commands like DIR have alias so they
will work just like in a command prompt. As shown here, when you run the command DIR you
are really running the CmdLet Get-ChildItem. The next point I want to look at is how variables
work in PowerShell. A variable is a storage location which contains information like values.
In the case of PowerShell, a variable can also contain an object.
All variables start with a dollar sign, for example I could create a variable called $DHCP.
The variable DHCP can then be used to store data or an object. In this case I will call
the CmdLet GetService. This CmdLet retrieves the object for a service. What has occurred
is the Get-Service CmdLet has retrieved the object for the service DHCP. In other words,
the variable DHCP now contains the object for the DHCP service. To make this a bit clearer,
see how I can add period status to the variable to retrieve the status of the DHCP service.
If I enter in $DHCP followed by a period, I can press the tab key to cycle through what
is available for that variable. You can see how you could create PowerShell scripts that
could read the status of services and access them accordingly. For example, if you had
an installation script, you could detect if a service is running and then stop the service
while the install is running. Once the install is complete it could than start the service
up again. The next task that I will perform is creating
a user in Active Directory. To do this I will retrieve the object for an organizational
unit where I want to create the user and store it in a variable. The variable I will use
is $OBJOU. This is followed by ADSI in square brackets followed by the LDap and the distinguished
name to where the organizational unit is located in Active Directory.
ADSI in square brackets references the ADSI adapter. Think of ADSI as being the same as
Get-Service CmdLet. It retrieves the object in the LDap query that follows. There are
other ways of achieving the same result using CmdLets, but these are more complicated than
using the ADSI syntax shown. Think of ADSI as the same as a CmdLet but the syntax is
different. I now have a variable OBJOU that contains
the object for the New York Organizational Unit. The next step is to create a new user
under that organizational unit. I will store the new user in a variable called OBJUser
by running the function create under the New York organizational unit. To do this, I use
the OBJOU variable followed by period create. This tells PowerShell to create an object
under the organizational unit stored in the variable, in this case New York.
Next you need to enter in the parameters for create. The first parameter is the object
type which is used to indicate which type of objects is to be created, in this case
a user. The next parameter is the user name which will be Charlie Wilson.
Once this command has been run what essentially happens is this. The variable OBJUser now
contains an object for the new user. After creating an object for the new user you will
be able to configure more options for the user.
This is done by using the variable OBJUser period Put. The Put function will configure
an attribute in the user object. In this case I am giving it a pre Windows 2000 name which
is referred to as a Sam Account Name. The Sam Account name is a username used with old
Windows clients like Windows 95. If I now open Active Directory Users and Computers
and open the New York OU, notice that the user account has not been created yet. What
has happened is that the object has been created but none of the changes have been committed
as yet. To commit the changes, I need to go back to
PowerShell and then run the function SetInfo. Once this has been done, if I now go back
to Active Directory Users and Computers and press the F5 key for refresh, you can see
that the user account has now been created. This is an important point to remember. Whenever
changing attributes in Active Directory, ensure that afterwards you commit the changes otherwise
the changes will be lost. The next task that I want to complete in PowerShell
is to modify some attributes in a user account that already exists in Active Directory. This
is a simpler process than creating a new user. Since the user already exists I can access
the user object directly using one line. I could not do this before, because the user
needed to be created first using the organizational unit object. For this reason I will store
the user object in the OBJUser variable. Next I will configure a number of difference
attributes for the user. The main attributes I will configure will be - given name, initials,
surname, display name, description, telephone number and email address.
Once I have finished making the changes remember that once again they need to be committed
using the SetInfo function. If you do not run this function after making the changes,
all the changes will be lost. This has been a short introduction to PowerShell.
There is a lot that can be achieved with PowerShell including more programming tasks like loops
and condition statements. I hope you found this video informative. For
more videos in this course please see our YouTube channel or ITfreetraining Webpage.
Thanks for watching.