0% found this document useful (0 votes)
20 views

User To Ad

Uploaded by

tvsinfinity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

User To Ad

Uploaded by

tvsinfinity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

To add a user to Active Directory (AD) using a PowerShell script, you

can use the New-ADUser cmdlet. Below is a sample script to add a new
user to Active Directory. It assumes you have the appropriate
permissions to add users to your domain and that the Active Directory
module for PowerShell is installed.
PowerShell Script to Add a User to Active Directory
powershell
Copy code
# Import the Active Directory module (if not already loaded)
Import-Module ActiveDirectory

# Define user details


$userFirstName = "John"
$userLastName = "Doe"
$userUsername = "johndoe"
$userPassword = "P@ssw0rd123"
$userOU = "OU=Users,DC=domain,DC=com" # Modify with the desired
OU in your domain
$userDisplayName = "John Doe"
$userEmail = "[email protected]"
$userDescription = "New employee in the IT department"

# Create the new user


New-ADUser -SamAccountName $userUsername `
-UserPrincipalName "[email protected]" `
-GivenName $userFirstName `
-Surname $userLastName `
-Name "$userFirstName $userLastName" `
-DisplayName $userDisplayName `
-Description $userDescription `
-EmailAddress $userEmail `
-AccountPassword (ConvertTo-SecureString $userPassword -
AsPlainText -Force) `
-Enabled $true `
-Path $userOU `
-PassThru

# Optional: Set the user to change password at next logon


Set-ADUser -Identity $userUsername -ChangePasswordAtLogon $true

# Output confirmation
Write-Host "User $userUsername has been created successfully."
Explanation of the Script:
1. Import-Module ActiveDirectory: Ensures the Active Directory
module is available in the current session. (The module is part of
RSAT tools, and you need it to run AD-related cmdlets).
2. User Details: The script uses variables to define the user's details
such as:
o First name, last name, username, password, email,
description, etc.
o Modify these variables with the correct values for your new
user.
3. New-ADUser: The cmdlet used to create a new user in Active
Directory. The parameters:
o SamAccountName: The username that is used for login.

o UserPrincipalName (UPN): This is usually the username with


the domain (e.g., [email protected]).
o GivenName: The user's first name.

o Surname: The user's last name.

o Name: The full name of the user.

o DisplayName: The name shown in the Active Directory.

o EmailAddress: The user's email address.

o AccountPassword: The password for the user, which is


converted to a SecureString to ensure security.
o Enabled: Whether the account should be enabled
immediately upon creation.
o Path: The Organizational Unit (OU) where the user should be
placed. Adjust it according to your AD structure.
4. Set-ADUser -ChangePasswordAtLogon: This optional line forces
the user to change their password the first time they log in.
5. Write-Host: Outputs a confirmation message to the console
indicating the user has been successfully created.

Running the Script


1. Open PowerShell as an Administrator.
2. Run the script by pasting it into the PowerShell console or saving
it as a .ps1 file and executing it.
3. Make sure to modify the script’s variables (like
OU=Users,DC=domain,DC=com, username, password, etc.) to
match your environment.
Important Notes:
 Password Complexity: Ensure the password you choose complies
with your domain's password policy.
 Permissions: You must have the necessary permissions to create
users in Active Directory.
 Organizational Unit (OU): Modify the -Path parameter to reflect
the correct Organizational Unit (OU) where the user should be
created. For example, OU=Users,DC=domain,DC=com.
Let me know if you need further adjustments or additional features in
this script!
4o mini

You might also like