PowerShell批量创建域账号需用CSV提供结构化数据,含DisplayName、SamAccountName、UserPrincipalName、Password、OUPath等字段,脚本导入ActiveDirectory模块,转密码为SecureString,调用New-ADUser并加错误处理。
准备CSV格式的员工信息文件
powershell批量创建域账号依赖结构化数据源,推荐使用csv文件。字段需包含必需信息:姓名(displayname)、登录名(samaccountname)、用户主体名称(userprincipalname,如 user@contoso.com)、密码(password)、所属ou(oupath),还可选填部门、邮箱、电话等扩展属性。
示例CSV内容(注意:首行为标题行,无空格或中文逗号):
DisplayName,SamAccountName,UserPrincipalName,Password,OUPath,Department 张三,zhangsan,zhangsan@contoso.com,P@ssw0rd2024,"OU=IT,OU=Employees,DC=contoso,DC=com",IT 李四,lisi,lisi@contoso.com,P@ssw0rd2024,"OU=HR,OU=Employees,DC=contoso,DC=com",HR
确保OU路径真实存在,可提前用 Get-ADOrganizationalUnit 验证;密码需满足域策略(长度、复杂度),建议统一生成强密码并后续通知用户重置。
编写核心PowerShell脚本
脚本需导入 ActiveDirectory 模块,并逐行读取CSV,调用 New-ADUser 创建账号。关键点包括:
– 密码需转为 SecureString
– 启用账号、设置初始密码不过期(可选)
– 添加错误处理,跳过失败项并记录日志
基础脚本示例(保存为 CreateADUsers.ps1):
# 导入模块
Import-Module ActiveDirectory
<h1>CSV路径</h1><p>$csvPath = "C:\data\employees.csv"</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/2647" title="GPTPLUS"><img
src="https://img.php.cn/upload/ai_manual/001/246/273/697dcaf3ac22c985.png" alt="GPTPLUS" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/2647" title="GPTPLUS" class="overflowclass">GPTPLUS</a>
<p class="overflowclass">一款AI翻译工具,主要用于GPTPLUS, 由GPT-4和GPT-3.5支持,为您的写作、翻译、代码分析和问答需求提供最准确、有效的AI反馈,适合需要提升相关任务效率的用户。</p>
</div>
<a rel="nofollow" href="/ai/2647" title="GPTPLUS" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><h1>读取数据</h1><p>Import-Csv $csvPath | ForEach-Object {
$securePass = ConvertTo-SecureString $<em>.Password -AsPlainText -Force
$params = @{
DisplayName = $</em>.DisplayName
SamAccountName = $<em>.SamAccountName
UserPrincipalName = $</em>.UserPrincipalName
AccountPassword = $securePass
Path = $<em>.OUPath
Enabled = $true
ChangePasswordAtLogon = $true # 强制首次登录改密
Department = $</em>.Department</p><h1>其他属性可按需添加,如: EmailAddress, TelephoneNumber</h1><pre class="brush:php;toolbar:false;">}
try {
New-ADUser @params
Write-Host "✅ 已创建 $($_.SamAccountName)" -ForegroundColor Green
} catch {
Write-Warning "❌ 创建失败 $($_.SamAccountName) : $($_.Exception.Message)"
Add-Content -Path "C:\data\import_errors.log" -Value "$($_.SamAccountName) : $($_.Exception.Message)"
}
}
执行前必做的权限与环境检查
脚本必须在具备足够权限的环境中运行:
- 以域管理员或具有“创建用户对象”权限的账户运行 PowerShell(右键 → “以管理员身份运行”)
- 目标域控制器网络可达,且已安装 RSAT-AD-PowerShell 功能(Windows Server 默认启用;Win10/11需手动添加)
- 确认当前会话能正常查询AD,测试命令:
Get-ADUser -Identity Administrator - 首次运行建议加
-WhatIf参数预览(仅适用于支持该参数的 cmdlet;New-ADUser不支持,可用Write-Host模拟输出代替)
增强实用性的小技巧
让脚本更健壮、易维护:
-
自动OU判断:根据部门字段动态拼接OU路径,例如
"OU=$($_.Department),OU=Employees,DC=contoso,DC=com",避免CSV中硬编码长路径 -
密码随机生成:用
[System.Web.Security.Membership]::GeneratePassword(12,2)(需先Add-Type -AssemblyName System.Web)替代固定密码 -
批量启用邮箱:若集成Exchange,可在创建后调用
Enable-Mailbox(需Exchange管理Shell) - 导出成功清单:将成功创建的账号写入新CSV,便于后续分发或审计










