Power Shell | Variable| Alias |How to Create Users Account in AD Using PowerShell

PowerShell server 2008

What is PowerShell?

PowerShell is a shell program by Microsoft to provide you a management console, use a new command prompt and scripting environment. PowerShell has a fundamental difference with the concept that uses VBScript and other programming languages.

 

PowerShell commands are called cmdlets. PowerShell comes with over 100 cmdlet. PowerShell also has many build in alias like dir, cd, del, copy etc.

How to install PowerShell?

You can install the PowerShell from add feature. Open the feature console for windows applications and Click on add PowerShell. Dot net frame work is prerequisite for PowerShell, that way you may not able to run the PowerShell from server core. <Server core link>

Some of commands are as follow:

Get-services (showing all services)

Stop service –name spooler

Start-service –name spooler

 

You can view the result in shell console or you can save these results as a script.

How to get help in PowerShell:

You don’t need to memorize all the commands in power shell you can get help and find the full syntax of any command from power shell, for example you want to learn about the “get-service” you need to type

Get-help get-service

It will show you all help regarding this command.

Variable in PowerShell:

Variable in PowerShell help you to reduce the typing of the long commands that you use most frequently. Suppose you mostly use the “stop service –name spooler” during your daily work, for ease you can create a short variable for this command and instead of typing whole command you can use that variable to run this command. Remember all the variables are start with $.

$p= service –name spooler

Now you can use the just $p instead of typing the whole command.

 

What is PowerShell Alias?

Alias shortened your commands, it is an abbreviation for long command. For example you can create an alias np for notepad with following command:

New-alias np notepad

You type the get-alias in PowerShell and you can get all the list of all aliases on give systems.

 

Please note that all own created variables and aliases are per session base, when you closed the PowerShell all you aliases and variables are also discard.

 

Create users with PowerShell from spreadsheet (Excel):

You can create active directory users from PowerShell in many ways. One way of creating is typing following commands in PowerShell.

 

$ou=[ADSI] “LDAP://ou=HR,ou=Department,dc=w7cloud,dc=com”

$newUser=$OU.create(”user”,”cn=M khan”)

$newUser.Put(”sAMAccountName”,”MKhan”)

$newUser.SetInfo()

Above command will create a user MKhan in HR organizational unit under the Department organizational unit, where w7cloud.com is domain name LDAP is representing the directory services.

 

Create user with .PS1 File in PowerShell:

You can also create users in active directory with a .PS1 file, a simple PS1 file may have the following code which is similar to above commands for creating users with PowerShell.

Users.PS1

$ou=[ADSI] “LDAP://ou=HR,ou=Department,dc=w7cloud,dc=com”

$newUser=$OU.create(”user”,”cn=M waqas”)

$newUser.Put(”sAMAccountName”,”MWaqas”)

$newUser.SetInfo()

$newUser.SetPassword(“W@q@$98765”)

$newUser.Put(“UserAccountControl”,”544”)

$newUser.SetInfo()

 

Download users.PS1 file for creating user accounts in active directory.

Above code create a user account with name M Waqas in HR OU in DC w7cloud.com and set a password  W@q@$111 and (“UserAccountControl”,”544”) made the user account enabled.

Copy and paste the above code in notepad and made changes according to your user and save this file with filename.PS1 on your DC. Now open PowerShell and go to directory where you save your .PS1 file and in PowerShell and run the following command (In my case users.PS1 file is save on my desktop.)

 

PS C:UsersAdministratorDesktop> .users.ps1

Also make sure you have enabled Script execution on PowerShell, you can enable Script execution by typing command Set-ExecutionPolicy unrestricted.

PowerShell Command Syntax | Create Users Account in AD 

Waqas Azam
Me Waqas Azam and I am a professional blogger & freelance writer. I also working in the IT industry for over 7 years. I am graduated in Computer Science and information technology.