powershell分享5--AD中常用的小语句

一、通过powershell批量创建邮箱

Set-Location C:\Users\fanyx_v\Desktop\

Import-Module ActiveDirectory
Add-PSSnapin Microsoft.Exchange*
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
$ADUser=Import-Csv -Path .\NewUser.csv -UseCulture -Encoding Default
foreach ($Users in $ADUser)
{
$UserPrincipalName=$Users.UserPrincipalName #每个用户帐户都有用户主体名称 (UPN),格式为:<user>@<DNS-domain-name>。#
#$Company=$Users.Company #指定用户所在的公司。#
$Department=$Users.Department #指定用户所在的部门。#
$DisplayName=$Users.DisplayName #指定对象的显示名称。#
$Name=$Users.Name #指定对象的名称。#
$SamAccountName=$Users.SamAccountName
$Description=$Users.Description #账户的描述信息#
$Manager=$Users.Manager #指定用户的经理。#
#$AccountPassword=$Users.AccountPassword # 为帐户指定新密码值。#
$MobilePhone=$Users.MobilePhone #指定用户的移动电话号码。
$AccountExpirationDate=$Users.AccountExpirationDate #指定用户的过期时间

    #根据需要添加
New-ADUser -Name $Name -Manager  $Manager  -AccountExpirationDate $AccountExpirationDate  -UserPrincipalName  $UserPrincipalName -Department  $Department   -SamAccountName $SamAccountName   -DisplayName  $DisplayName  -Description $Description   -ChangePasswordAtLogon 0 -AccountPassword (ConvertTo-SecureString "2q3eNChZ" -AsPlainText -Force) -Enabled 1 -Path "OU=测试公司,DC=demo,DC=com"
sleep 20 #延迟数
Enable-Mailbox  $users.SamAccountName -Database "database"    

}

二、导出指定OU下面所有人的一些信息

Get-ADUser -Filter -SearchBase "OU=测试公司,DC=demo,DC=com" -Properties | select Name,whenCreated,PasswordLastSet,SamAccountName | Export-CSV creat_time.csv -NoTypeInformation -Encoding UTF8

三、批量创建联系人

import-csv C:\Users\fanyxv\Desktop\contact.csv | ForEach {new-mailcontact -Alias $.Alias -name $.displayname -ExternalEmailAddress $.targetaddress}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章