Get the App
SLTechnology News&Howtos  ›  Servers  › 

Bulk import AD users using powershell

Shulou Source: shulou.com Published: 2022-06-02 05:33:36 10月01日 Update

Save the text document as a csv file and pay attention to the coding format, otherwise it will be garbled after import.

Use the powellshell command to import

Import-Csv-Path E:\ users.csv | foreach {new-aduser-name $_ .name-SamAccountName $_ .samaccountname-GivenName $_ .givenname-Surname $_ .surname-DisplayName $_ .displayname-Path $_ .path-UserPrincipalName $_ .userprincipalname-AccountPassword (ConvertTo-SecureString-string $_ .accountpassword-AsPlainText-Force)-Enabled $true-ChangePasswordAtLogon $false}

3. View established users

Other:

Export user's command

The first step is to get all the OU commands on AD: Get-ADOrganizationalUnit-Filter 'Name-like "*"

The second part is to obtain user commands according to OU:

Get-ADuser-searchbase 'OU=IT,DC=dareway,DC=com'-filter' name-Like "*"-Properties * | Export-Csv c:\ sharepointinfo.csv-enconding utf8

-

Name,SamAccountName,GivenName,Surname,DisplayName,Department,UserPrincipalName,City,Office,StreetAddress,State,PostalCode,Title,Company,OfficePhone,HomePhone,MobilePhone,emailaddress

- -

Get the required fields in the user information:

Get-ADuser-searchbase 'OU=IT,DC=dareway,DC=com'-filter' name-Like "*"-Properties name,SamAccountName,GivenName,Surname,DisplayName,Department,UserPrincipalName,City,Office,StreetAddress,State,PostalCode,Title,Company,OfficePhone,HomePhone,MobilePhone,emailaddress | Export-Csv c:\ sharepointinfo.csv-enconding utf8

Import user's command

Open the CSV file in notepad and save it as Unicode encoding

PS C:\ Users\ Administrator\ Desktop > Import-Csv "e:\ file.csv" | foreach-object {New-ADUser-Path "ou=IT,dc=dareway,dc=cn"

-Name $_ .name-SamAccountName $_ .SamAccountName-GivenName $_ .GivenName-Surname $_ .Surname-DisplayName $_ .DisplayName

-Department $_ .Department-UserPrincipalName $_ .UserPrincipalName-City $_ .City-Office $_ .Office-StreetAddress $_ .Stre

EtAddress-State $_ .State-PostalCode $_ .PostalCode-Title $_ .Title-Company $_ .Company-OfficePhone $_ .OfficePhone-Hom

EPhone $_ .HomePhone-MobilePhone $_ .MobilePhone-emailaddress $_ .emailaddress-AccountPassword (ConvertTo-SecureString "

Dareway@2018 "- AsPlainText-force)-enabled $true-ChangePasswordAtLogon $true-PasswordNeverExpires $false-Passthru}

- -

Randomly generate initial passwords

Import-Csv "e:\ file.csv" | foreach-object {New-ADUser-Path "ou=IT,dc=dareway,dc=cn"-Name $_ .name-SamAccountName $_ .SamAccountName-GivenName $_ .GivenName-Surname $_ .Surname-DisplayName $_ .DisplayName

-Department $_ .Department-UserPrincipalName $_ .UserPrincipalName-City $_ .City-Office $_ .Office-StreetAddress $_ .Stre

EtAddress-State $_ .State-PostalCode $_ .PostalCode-Title $_ .Title-Company $_ .Company-OfficePhone $_ .OfficePhone-Hom

EPhone $_ .HomePhone-MobilePhone $_ .MobilePhone-emailaddress $_ .emailaddress-AccountPassword (ConvertTo-SecureString $_ .password-AsPlainText-force)-enabled $true-ChangePasswordAtLogon $False-PasswordNeverExpires $false-Passthru}

- -

Get all user accounts under OU Beijing (including sub-OU)

Get-user-OrganizationalUnit IT | where-object {$_ .RecipientType-eq "User"}

Batch enable users

Get-user-OrganizationalUnit IT | where-object {$_ .RecipientType-eq "User"} | Enable-Mailbox-Database "Mailbox Database 0839106375" | get-mailbox | select name,windowsemailaddress,database

Tags: User command file code garbled information field password format type notepad account part II generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi MySQL NVidia Microsoft