Create An Active Directory User and Add To Group Using PowerShell

In this blog post, I will show you how I create a new Active Directory user and add him to an Active Directory Group using PowerShell.

About the Script

The PowerShell code in the script will start by asking you to provide a password for the user, and in my case, I am going to create a user called Ansible.

After the script creates the user, the 3rd line will add the user to the Domain Admins group in Active Directory.

The last line will display all the group members of the Domain Admins group.

$pass = Read-Host "Enter password" -AsSecureString

New-ADUser -Name "Ansible Service" -AccountPassword $pass -UserPrincipalName ansible -DisplayName "Ansible Service Account" -SamAccountName ansible

Add-ADGroupMember -Identity "Domain admins" -Members ansible

Get-ADGroupMember -Identity "domain admins" | Select-Object name

Success! You're on the list.