Powershell Bulk User Import in Server 2012

简介:

Modify CSV File

Now that you have a CSV file with all your users and extra information, we need to format it so that certain fields can be imported. We will also need to add a few fields. For a list of all fields that Powershell can import check out Microsoft’s TechNet Article. In my file I used the following columns:

  • Username

  • GivenName (or first name)

  • MiddleInitial

  • Surname (last name)

  • Display Name (Full Name)

  • UserPrincipalName

  • StreetAddress

  • City

  • State

  • ZipCode

  • Password

  • TelephoneNumber

  • Occupation

  • Path

  • Office

Some of these items I want to go into more detail about because they are tricky to make.

  • Username, for my domain, will consist of First initial + Last name. So I created a new column in my CSV file and used this function to create the username: =CONCATENATE(LEFT(B2,1), D2) Where as B is the GivenName column and D is the Surname column. Example: Danny Eckes becomes DEckes.

  • UserPrincipalName, is the logon name and domain on the Account tab of an Active Directory user properties. Since this one column fills two items you need to make sure this is done correctly. The function I used for this column is =A2&”@eckescorp.local” Where as A is the Username column and the text @eckescorp.local is the name of my fake domain. Active directory will use the @ to separate the two items. If you misspell something here you can run into trouble. Example: DEckes becomes DEckes@eckescorp.local.

  • DisplayName, for my domain, is First name and Last name. You can also add Middle Initial if you wanted. However when looking at the Name field of Active Directory, First and Last name look fine for me. The function I used for DisplayName is: =B2&” “&D2 Where B is the GivenName column and D is the Surname column. Note that the quotes have a space in the middle of them. This separates the first and last name. Example: Danny Eckes becomes one cell of Danny Eckes.

  • Password, this column is the value I will use for the password. Since I am doing this for testing and I want all users to have the same password I just set it to Password123.

  • Path is the Active Directory Path to the Organizational Unit the user object will be created. It helps to read this from right to left. OU=Los Angeles,OU=ecorp – Users,DC=eckescorp,DC=local when read means to create the new user on the Eckescorp.local Domain Controller in side the Los Angeles Organizational Unit that is a child of the ecorp – Users Organizational Unit. You will need to adjust this to match your DC tree structure.

You can take a look at my completed CSV file here.

Create Powershell Script

Now that we have our CSV file we can create a script that will import specific AD values from the columns in the CSV file. Open up Notepad and save the file as a .ps1 file. Name it whatever is easiest for you. Your script will consist of 2 lines:

  • Line 1: Import the CSV file and for each row of the CSV do the next line. Make sure you have the CSV filename and path correct. In my case the CSV and the script are in the same folder.

  • Line 2: Match the AD fields to the columns of the CSV. -FIELDNAME is the AD object and $_.COLUMNNAME is the item from the CSV. Example -Name $_.DisplayName will place the value of the cell in the DisplayName column as the Name in Active Directory.

    • -AccountPassword is a tricky one to set. I would suggest copying mine. It pulls the cell from the Password column and converts it to a secure string which is required for passwords.

    • -Enabled $True sets the account as enabled.

    • -PasswordNeverExpires $True will set the option for password on this account to NOT expire.

For simplicity you can use the following script:

PowerShell

Import-Csv .\userImport.csv | foreach-object { New-ADUser -Name $_.DisplayName -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.Username -GivenName $_.GivenName -DisplayName $_.DisplayName -Initials $_.MiddleInitial -SurName $_.Surname -Description $_.Description -Department $_.Department -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.ZipCode -HomePhone $_.TelephoneNumber -Title $_.Occupation -Office $_.Office -Path $_.Path -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru }

1

2

Import-Csv .\userImport.csv | foreach-object {

New-ADUser -Name $_.DisplayName -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.Username -GivenName $_.GivenName -DisplayName $_.DisplayName -Initials $_.MiddleInitial -SurName $_.Surname-Description $_.Description -Department $_.Department -StreetAddress $_.StreetAddress -City $_.City -State$_.State -PostalCode $_.ZipCode -HomePhone $_.TelephoneNumber -Title $_.Occupation -Office $_.Office -Path $_.Path -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru }


Run PowerShell Script

If you haven’t yet, you will need to make sure you have enabled Powershell scripts on your server. Follow my article here to do that. Put your CSV files and Powershell script on your DC, or server that you have Active Directory installed on. I put them in C:\Temp but if you’re going to run a lot of scripts you might want to create a C:\Scripts folder.

Launch Powershell as an Administrator. Then at the prompt change directory “CD” to get to the folder that holds your scripts. Once in that folder, type:

PowerShell

.\ScriptName.ps1

1

.\ScriptName.ps1

Screen Shot 2013-08-24 at 5.05.30 PM

The Script will run and in seconds import all your users. Head over to Active Directory Users and Computers to confirm all the users are in the right locations and have the right attributes.

Screen Shot 2013-08-24 at 3.14.52 PM

I ran into several snags while importing these users. I first ran the script with just 2 users in my CSV file until I got all the kinks ironed out. I then imported the larger CSV file. I hope this explanation helps you get a basic understanding of Powershell New-ADUser Bulk Importing. Feel free to leave comments below.

- See more at: http://www.dannyeckes.com/powershell-bulk-user-import-in-server-2012/#sthash.igYnT1oN.dpuf

本文转自学海无涯博客51CTO博客,原文链接http://blog.51cto.com/549687/1868503如需转载请自行联系原作者


520feng2007

相关文章
|
6天前
|
人工智能 运维 安全
|
4天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
5天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
516 14
|
11天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
887 109
|
5天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。