PowerShell 批量导入AD域用户

EXCEL 另存为 CSV,,UTF-8(一定是将文档放到你需要执行命令的地方,然后在改文件的编码UTF-8,如果先改保存后,在移动到你执行命令的地方编码格式是无效的)

 

密码写在CSV里

PS C:\Windows\system32> import-csv c:\ad\User.csv | Foreach {New-ADUser  -samAccountName $_.SamAccountName  -Surname $_.Surname   -GivenName $_.GivenName  -Name $_.Name  -UserPrincipalName $_.Userprincipalname  -DisplayName $_.DisplayName  -Description $_.Description   -Path $_.Path  -Enabled $true -AccountPassword (ConvertTo-SecureString $_.AccountPassword -AsPlainText -force) -passthru -PasswordNeverExpires ($_.PasswordNeverExpires -eq "1")}

 

 

 

密码写在脚本里(123@abc)

PS C:\Windows\system32> import-csv c:\ad\User.csv | Foreach {New-ADUser  -samAccountName $_.SamAccountName  -Surname $_.Surname   -GivenName $_.GivenName  -Name $_.Name  -UserPrincipalName $_.Userprincipalname  -DisplayName $_.DisplayName  -Description $_.Description   -Path $_.Path -AccountPassword(ConvertTo-SecureString "123@abc" -AsPlainText -Force) -Enabled $true -ChangePasswordAtLogon 1  -passthru -PasswordNeverExpires ($_.PasswordNeverExpires -eq "1") }

 

表格样本:

 

 

 

#新建用户

import-csv d:\AD\Users.csv | Foreach {New-ADUser -Name $_.Name -Path $_.Path -samAccountName $_.SamAccountName -UserPrincipalName $_.Userprincipalname -Enabled $true -AccountPassword (ConvertTo-SecureString $_.AccountPassword -AsPlainText -force) -passthru -PasswordNeverExpires ($_.PasswordNeverExpires -eq "1")}

#删除用户

get-aduser -filter * -SearchBase "ou=oo,ou=admins,dc=ddv,dc=com" |? {$_.name -eq "test02"} | Remove-ADUser -Confirm:$false

#新建组

以命令行新建:

New-ADGroup -Name 04-00-01-Primary-Admin-RO  -GroupCategory Security -GroupScope Global -Path "OU=04-fmsz-NTFS-Security-Groups,DC=fmsz,DC=frameway,DC=com" 

 

以文件导入新建:

Import-Csv C:\ad\Groups.csv | foreach {New-ADGroup -Name $_.Name  -GroupCategory $_.GroupCategory -GroupScope $_.GroupScope -Path $_.Path -Description $_.description}

-Description $_.description(备注信息如果不要,表格里也要删除)

文件列表截图

 



#将用户加入组
import-csv d:\AD\Members.csv | Foreach {Add-ADGroupMember -Identity $_.Memberof -Members $_.Users}

#新建OU
新建一个CSV文件 第一列为Name,第二列为path 方法与安全组相同

import-csv c:\AD\OU.csv | Foreach {New-ADOrganizationalUnit -Name $_.Name -Path $_.Path}

 

#导入组策略
import-csv C:\ADPS\GPO.csv | Foreach {import-gpo -BackupId $_.BackupID -TargetName $_.TargetName -path "C:\ADPS\GPO\" -CreateIfNeeded}
#链接组策略
import-csv C:\ADPS\GPLink.csv | Foreach {New-GPLink -Name $_.TargetName -Target $_.OU -LinkEnabled Yes}

 

#导出所有的OU到CSV

Get-ADOrganizationalUnit -Filter 'Name -like "*"'  | Export-Csv c:\sharepointinfo.csv  

 

posted on 2021-06-23 23:24  stoneyeung  阅读(1738)  评论(0编辑  收藏  举报

导航