Active Directory
--
Questions
--
Followers
Top Experts
I think we have this as close as we can but we are getting errors that I can't put my finger on. May we get some help with this?
I have broken the following in three sections.
PowerScript
Errors
CSV file that we are importing
Powerscript
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\inetpub\ftproot\bulk_us
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$Firstname = $User.firstname
$Lastname = $User.lastname
$Username = $User.username
$Password = $User.password
$OU = $User.ou #This field refers to the OU the user account is to be created in
#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, give a warning
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#User does not exist then proceed to create the new user account
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@AZC.local" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False
}
}
__________________________
Error
PS C:\Users\Administrator\Des
New-ADUser : Directory object not found
At C:\Users\Administrator\Des
+ New-ADUser `
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (CN=Robert Maxfi...DC=AZC,DC=local:St
ing) [New-ADUser], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Micr
ment.ADIdentityNotFoundExc
ewADUser
New-ADUser : Directory object not found
At C:\Users\Administrator\Des
+ New-ADUser `
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (CN=Marcelino Or...DC=AZC,DC=local:Str
ing) [New-ADUser], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Micr
ment.ADIdentityNotFoundExc
ewADUser
New-ADUser : Directory object not found
At C:\Users\Administrator\Des
+ New-ADUser `
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (CN=Nickolas Bir...DC=AZC,DC=local:Str
ing) [New-ADUser], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Micr
ment.ADIdentityNotFoundExc
ewADUser
__________________________
CSV File
firstname lastname username password ou
Robert Maxfield rmaxfield vbaTFt8v CN=User,OU=Restricted Users,OU=AZC,DC=AZC,DC=loc
Marcelino Ornelas mornelas u29trASj CN=User,OU=Restricted Users,OU=AZC,DC=AZC,DC=loc
Nickolas Birch nbirch umV3q8FJ CN=User,OU=Restricted Users,OU=AZC,DC=AZC,DC=loc
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
view attachment Removeapostrophe.jpg
Removeapostrophe.JPG
Thanks so much for the keen eye and fast answer. You are amazing.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
May I ask one more question? Is there a way to specify and account expiration date?
And is it possible to put them as members of multiple AD domain services folder?
This is so far above me.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
#Loop through each row containing user details in the CSV file
Import-Csv C:\inetpub\ftproot\bulk_users1.csv | ForEach-Object {
Write-Host "Processing user '$($_.Username)' ..."
#Check to see if the user already exists in AD
If (Get-ADUser -Filter "SamAccountName -eq '$($_.Username)'") {
Write-Warning "A user account with username $($_.Username) already exist in Active Directory."
} Else {
#User does not exist then proceed to create the new user account
$splat = @{
SamAccountName = $_.Username
UserPrincipalName = "$($_.Username)@AZC.local"
Name = "$($_.Firstname) $($_.Lastname)"
GivenName = $_.Firstname
Surname = $_.Lastname
Enabled = $true
DisplayName = "$($_.Lastname), $($_.Firstname)"
Path = $_.OU
AccountPassword = (ConvertTo-SecureString -String $_.Password -AsPlainText -Force)
ChangePasswordAtLogon = $false
AccountExpirationDate = $_.AccountExpirationDate
}
New-ADUser @splat
Write-Host "... user created successfully."
}
}
And is it possible to put them as members of multiple AD domain services folder?Don't know what you mean with that, sorry. An AD user can not be stored in multiple OUs, if that's what you mean.
# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
$groupDelim = ','
#Loop through each row containing user details in the CSV file
Import-Csv C:\inetpub\ftproot\bulk_users1.csv | ForEach-Object {
Write-Host "Processing user '$($_.Username)' ..."
# Check to see if the user already exists in AD
If (Get-ADUser -Filter "SamAccountName -eq '$($_.Username)XXX'") {
Write-Warning "A user account with username $($_.Username) already exist in Active Directory."
} Else {
# User does not exist then proceed to create the new user account
$splat = @{
SamAccountName = $_.Username
UserPrincipalName = "$($_.Username)@AZC.local"
Name = "$($_.Firstname) $($_.Lastname)"
GivenName = $_.Firstname
Surname = $_.Lastname
Enabled = $true
DisplayName = "$($_.Lastname), $($_.Firstname)"
Path = $_.OU
AccountPassword = (ConvertTo-SecureString -String $_.Password -AsPlainText -Force)
ChangePasswordAtLogon = $false
AccountExpirationDate = $_.AccountExpirationDate
}
New-ADUser @splat
If ($_.MemberOf) {
Add-ADPrincipalGroupMembership -Identity $_.Username -MemberOf $_.MemberOf.Split($groupDelim).Trim()
}
Write-Host "... user created successfully."
}
}






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Active Directory
--
Questions
--
Followers
Top Experts
Active Directory (AD) is a Microsoft brand for identity-related capabilities. In the on-premises world, Windows Server AD provides a set of identity capabilities and services, and is hugely popular (88% of Fortune 1000 and 95% of enterprises use AD). This topic includes all things Active Directory including DNS, Group Policy, DFS, troubleshooting, ADFS, and all other topics under the Microsoft AD and identity umbrella.