Powershell Script New-AdUser CSV feedback

Hey Guys,

I wanted to thank Paul for getting me started in IT, Powershell ftw.

what your guys feedback on this script

# Import required  modules
Import-Module ActiveDirectory

# Prompt user for CSV file path
$filepath = "C:\ps1\newusers1.csv"

# Import File to variable
$users = Import-Csv $filepath


# Loop through each row and gather information
ForEach ($user in $users) {

    # Gather the user's information
   $uname = $users.UserPrincipalName
   $name = $user.Name
   $givenname = $user.GivenName
   $surname = $user.Surname
   $dname = $user.Displayname
   $saccount = $user.SamAccountname
   $office = $user.Office
   $department = $user.Department
   $manager = $user.Manager
   $job = $user.Title
   $company = $user.Company
   $address = $user.StreetAddress
   $city = $user.City
   $state = $user.State
   $postal = $user.Postalcode
   $country = $user.Country
   $ou = $user.'Organizational Unit'


   # Ad Attributes
   $Attributes = @{

   Enabled = $true
   ChangePasswordAtLogon = $true
   AccountPassword = "totalsecurepassword1234" | ConvertTo-SecureString -AsPlainText -Force
   Path = $ou
   UserPrincipalName = $uname
   Name = $name
   GivenName = $givenname
   Surname = $surname
   DisplayName = $dname
   SamAccountname = $saccount
   Office = $office

   
   Department = $department
   Manager = $manager
   Title = $job
   Company = $company
   StreetAddress = $address
   City = $city
   State = $state
   Postalcode = $postal
   Country = $country

}

  # Create new AD user for each user in CSV file
  New-ADUser @Attributes
  
  # Echo output for each new User
  echo "Account has been created for $dname in $ou"

}

That’s my first script :wink: with CSV import.

Hi @Pilot.oezeren

Nice to hear you like PowerShell.

Without running the script, I can see you are expanding it to add as much information as possible to the User objects, which is nice. Can you share the newusers1.csv file to test?

The Prompt user for the CSV file path is fixed. It might be a disadvantage if the CSV file is not on that path or has a different name. Other than that, it looks great.

Ricardo

Hey Recardo,

The reason I added so many attributes is because this script is actually now used/ intended for an Productive environment.

I’m responsible for the staff management in the APAC,EMEA & APAC region for a Cooperation that has around 4000 users.

In a month I get around 30-80 users to be created :).

Currently thinking to get a webhook API that connects our ITSM tool to the CSV.

I removed the fixed path on the Production system.

I will share the CSV later, plus I had to fix the UserPrincipal in the script with ‘UserPrincipal’ as it couldn’t processed it correctly when entering an email.

I see it makes sense. With that amount of users, it makes sense to have them created via a script. You might also want to send the results to a text file.

That’s my next plan :wink: after my vacation I gonna start working on that .

I remember the first time taking the Course via Udemy und the Second time via SA.

Happy I learned so much here , that got me my way into IT

@ricardo.p here the csv CSV

Great to hear that @Pilot.oezeren we are glad you have learned so much :sunglasses:

Btw, the link to the script did not work.

Thanks

Ricardo

sorry, had to change my Policy settings @ricardo.p

NewUsers1.csv

Thanks, I’ll check it out.