SharePoint Automation Deployment: How to use PowerShell to export/import users in AD

Source: Internet
Author: User
Tags ldap samaccountname

These months have been to help customers change demand, deployment. I have been exhausted, after a period of groping, I have some experience with PowerShell automation deployment, such as the use of PowerShell export to import the user in the ad. In the development of SharePoint platform, the use of ad for personnel management, will generally build organizational unit (ORGANIZATIONALUNIT) to manage users. When the final deployment to the customer's server, how to synchronize the user data in the local ad to the server, you know, if manually input personnel is a problem. Fortunately, PowerShell can help us solve this problem.

Export ad Users (export-csv)

First export the user Object in the local appropriate OU

. Description
Exports the person data in CSV format from the organizational unit in AD
. Example
. \userinfoexport.ps1-ou "Sources"-DC "xcgov"-path "c:\temp\xxx.csv" #> param
([string] $ou, [ String] $DC, [string] $path)
if (!) ( get-pssnapin| Where-object{$_. Name-eq "Microsoft.SharePoint.PowerShell"}) {
    add-pssnapin "Microsoft.SharePoint.PowerShell"
$ Searchbase= ' ou= ' + $ou + ', dc= ' + $DC + ', dc=com '
get-aduser-ldapfilter ' (name=*) '-searchbase $searchBase | Select givenname,surname,name,samaccountname| Export-csv $path-notypeinformation-encoding UTF8

Note the encoding format when exporting, especially if the data contains Chinese. For example, I use UTF-8 here.

The exported object contains a number of properties, and we select important attribute exports, such as givenname, SurName, Name, and sAMAccountName, and the results are as follows:

See more highlights of this column: http://www.bianceng.cn

Import ad Users (import-csv)

When you get the user in the specified OU, the next step is to import to the OU in the online server ad-specified

<#. Description imports the person information from the specified CSV format. Example. \importuserinfo.ps1-ou "Hello"-DC "Kingdom"-sourcepath "C:\temp\xxx.csv" #> param ([string] $ou, [ String] $DC, [string] $sourcePath) if (!) ( get-pssnapin| Where-object{$_.
Name-eq "Microsoft.SharePoint.PowerShell"}) {Add-pssnapin "Microsoft.SharePoint.PowerShell"} #创建组织单元 # [string] $path = ' ou= ' + $ou + ', dc= ' + $DC + ', dc=com ' if (![ Adsi]::exists ("ldap://$path")) {$DOMAINOBJ =[adsi] ("ldap://dc=" + $DC +, dc=com) $domainOU = $domainObj. Create (  "OrganizationalUnit", "ou=" + $ou) $domainOU. SetInfo ()} $users =import-csv-path $sourcePath foreach ($user in $users) {$givenName = $user. givenname $surName = $user. SurName $name = $user. Name $samAccountName = $user. sAMAccountName $userPrincipalName = $samAccountName + ' @ ' + $dc + '. com ' $password = $user. Password #创建AD user# new-aduser-name $name-samaccountname $samAccountName-userprincipalName $userPrincipalName-displayname $name-givenname $givenName-surname $surName-accountpassword ( convertto-securestring $password-asplaintext-force)-passwordneverexpires $true-enabled $true-path $path} Wri Te-host "Import succeeded"

If the code is always garbled when you import it, it is the correct encoding when you open the CSV format in Excel. Try to open the CSV in Notepad and save it as a UTF-8 format. Because I am using the English version of the server, I do not know whether the Chinese version of the system will be imported garbled.

After the import succeeds, as follows:


This blog has a head Start for PowerShell automation deployments, and the next essay is going to write. Use SPSD to automate deployment SharePoint, including using PowerShell to create permission groups, assigning permissions, setting up master pages, and so on.

Author information: Cnblogs Wood Wancheng Master

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.