AD-Add Users for Powershell

如何批量增加 AD Users?

首先你要有一個專有格式的 CSV

AD User 的屬性,您可以去微硬的官網,我這裡有一堆可供參考的屬性,name,SamAccountName,GivenName,Surname,DisplayName,Department,UserPrincipalName,City,Office,StreetAddress,,State,PostalCode,Title,Company,OfficePhone,,HomePhone,MobilePhone,emailaddress,這些屬性都是對應上圖的

1、将 CSV 文件用记事本打开,并另存为 UFT-8 编码,要不然會亂碼

PS C:\Users\ian> Import-Csv "c:\ad\file2.csv" | foreach-object {New-ADUser -Path "ou=IT,dc=ianext,dc=local" -Name $_.name -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Surname $_.Surname -DisplayName $_.DisplayName -Department $_.Department -UserPrincipalName $_.UserPrincipalName -City $_.City -Office $_.Office -StreetAddress $_.StreetAddress -State $_.State -PostalCode $_.PostalCode -Title $_.Title -Company $_.Company -OfficePhone $_.OfficePhone -HomePhone $_.HomePhone -MobilePhone $_.MobilePhone -emailaddress $_.emailaddress -AccountPassword (ConvertTo-SecureString -AsPlainText "1Qaz@4321wsx" -Force) -enabled $true -ChangePasswordAtLogon $true -PasswordNeverExpires $false -Passthru}

導入成功

登陸 Windows 的時候,提示修改密碼

幾秒鐘的功夫,就可以匯進幾百個指定的 Users 信息到 AD 指定的 OU 裡

1、CSV 文件編碼必須是 UFT-8 編碼
2、ou,必須存在在 AD 上,要不然會說找不到 ou
3、dc=ianext,dc=local,請改為你自己的 AD Domain
4、默認的登陸密碼是 1Qaz@4321wsx
4、用戶第一次登陸後,需設置新的密碼

導出 AD 賬號信息

PS C:\Users\ian> Get-ADUser -Filter * -Properties * -SearchBase "DC=ianext,DC=local" |Select-Object name,SamAccountName,Givenname,surname,Displayname,title,mobile,CanonicalName,Created,Department,DistinguishedName,EmailAddress,homeMDB,mail,mailNickname,MemberOf,msExchCoManagedObjectsBL,msExchHomeServerName,PasswordLastSet,PrimaryGroup,proxyAddresses,UserPrincipalName,whenCreated,whenChanged,MobilePhone,telephoneNumber,employeeNumber,postalCode,company | Export-Csv C:\AllADUsers.csv -Encoding UTF8 -NoTypeInformation

1、導出的 CSV 文件是 UFT-8 編碼
2、dc=ianext,dc=local,請改為你自己的 AD Domain

批量添加 OU

 new-adorganizationalunit
PS C:\Users\ian> new-adorganizationalunit  -name "Administration Services" –path "ou=it,dc=ianext,dc=local"
-name,如果你的 OU 名字含有 -、空格等其它標識符,請用 “ ”
-path,“ ” 指定的 OU 名稱

查閱所有 OU 信息

查閱所有 OU 信息
PS C:\Users\ian> get-adorganizationalunit -filter * |select distinguishedname

Template
1、在 OU “IT” 下 Administration Services,
2、創建全局安全組,
3、名字為 Administration Services,
4、描述為 行政服务 

PS C:\Users\ian> New-ADGroup -Name "Administration Services" -SamAccountName "Administration Services"  -GroupCategory Security -GroupScope Global -Path "ou=Administration Services,ou=it,DC=ianext,DC=local" -Description "行政服务 "

Note: -SamAccountName 不能含有 “.” 標識符,要不然 Powershell 會提示名稱不是正確格式

 添加指定賬號 ian 到指定的 Group
PS C:\Users\ian> Add-ADGroupMember -Identity "Administration Services" -Members ian

 添加指定多個賬號到指定的 Group
PS C:\Users\ian> Add-ADGroupMember -Identity "Administration Services" -Members "Ternence.Sophia","Annabelle.Edmund"

 查詢用戶 ian,在那個 Group
PS C:\Users\ian> Get-ADPrincipalGroupMembership -Identity ian

 查詢指定 Group 都有哪些成員
PS C:\Users\ian> Get-ADGroupMember -Identity "Administration Services" | select name

Templates
創建 OU “A”
PS C:\Users\ian>  new-adorganizationalunit  -name "A" -path "dc=ianext,dc=local"
創建 Group “A-GP”
PS C:\Users\ian>  New-ADGroup -Name "A-GP" -SamAccountName "A-GP"  -GroupCategory Security -GroupScope Global -Path "ou=A,DC=ianext,DC=local" -Description "行政服务 "
創建 User “I”,並手動輸入用戶的密碼“********”
PS C:\Users\ian>  New-ADUser -Name "Ian Tse" -GivenName "Ian" -Surname "Tse" -SamAccountName "I" -UserPrincipalName "I@ianext.local" -Path "OU=A,DC=ianext,DC=local" -AccountPassword(Read-Host -AsSecureString "Input Password") -Enabled $true 


Windows Server 2016-OU组织单位日常操作

版权声明:
作者:ianext
链接:https://www.ianext.com/ad-add-users-for-powershell/
来源:粵分享越快樂
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>