AD 新建用户

New-ADUser -SamAccountName hexiaomin \
-Name 何小闵 \
-UserPrincipalName hexiaomin@hxx.tt \
-DisplayName 何小闵 \
-Office 集团创意中心 \
-EmailAddress hexiaomin@hxx.tt \
-Title 信息发展创意工程师 \
-Department 信息发展中心 \
-Manager heyixuan \
-Path “OU=创意中心,OU=用户组,OU=集团,DC=hxx,DC=tt” \
-AccountPassword (convertto-securestring “hxm^-^10024” -asplaintext -force) \
-PasswordNeverExpires $true \
-Enabled $true

Exchange 新建邮箱

直接新建邮箱和用户

$Mailbox02 = @{
Name = ‘武思瑜’
Password = (Convertto-Securestring “N#J8xoO&” -Asplaintext -Force)
UserprincipalName = ‘wusiyu@thtfpc.com’
Alias = ‘wusiyu’
DisplayName = ‘武思瑜’
Lastname = ‘武’
FirstName = ‘思瑜’
Database = ‘DB1603’
OrganizationalUnit = ‘OU=国际信息技术(苏州)有限公司,OU=05国际(计算机)公司,OU=北京产业楼,DC=abc,DC=com’
ResetPasswordOnNextLogon = $false
SamAccountName = ‘wusiyu’
}
New-Mailbox $Mailbox02

用户创建后再启用邮箱

查询 新建用户上级所在OU

Get-ADUser -Identity yuanjixin

新建用户后再启用邮箱

$splat = @{
Name = ‘武思瑜’
AccountPassword = (Convertto-Securestring “N#J8xoO&” -Asplaintext -Force)
Enabled = $true
PasswordNeverExpires = $true
SamAccountName = ‘wusiyu’
UserprincipalName = ‘wusiyu@thtfpc.com’
DisplayName = ‘武思瑜’
Office = ‘办公产品中心’
EmailAddress = ‘wusiyu@thtfpc.com’
Title = ‘产品工程师’
Department = ‘移动产品部’
Manager = ‘yuanjixin’
Path = ‘OU=国际信息技术(苏州)有限公司,OU=05国际(计算机)公司,OU=北京产业楼,DC=abc,DC=com’
}
New-ADUser @splat

启用邮箱

$Mailbox01= @{
Identity = ‘wusiyu’
Alias = ‘wusiyu’
DataBase = ‘DB1603’
}
Enable-Mailbox @Mailbox01

查询用户所在的通讯组

Get-ADPrincipalGroupMembership wusiyu | select Name,GroupCategory
Get-ADPrincipalGroupMembership yuanjixin | select Name ,GroupCategory

添加通讯组成员

Add-DistributionGroupMember -Identity ‘同方国际北京全体员工’ -Member wusiyu

批量创建exchange邮箱

定义一个统一密码:password
$MailPasswd =ConvertTo-SecureString password -AsPlainText -Force
2.新建CSV文件NewMail.csv,格式如下
LastName,UserPrincipalName,Database,Alias,DisplayName,OrganizationalUnit,FirstName,Name,

LastName :姓
UserPrincipalName :邮箱地址
Database :指定数据库
Alias :别名
DisplayName :显示名称
OrganizationalUnit :指定一个OU
FirstName :名
Name :姓名

以管理员身份运行:Exchange Management Shell,运行下面命令

Import-Csv D:\test\NewMail.csv | ForEach-Object -Process {New-Mailbox -ResetPasswordOnNextLogon:$False -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -Database $_.Database -Alias $_.Alias -Password $MailPasswd -DisplayName $_.DisplayName -OrganizationalUnit $_.OrganizationalUnit -FirstName $_.FirstName -Name $_.Name}

批量创建邮箱用户

Import-Module activedirectory

Store the data from ADUsers.csv in the $ADUsers variable


SamAccountName	Password	Name	Path	Department	DisGroup	userPrincipalName	Title	Displayname	Description	EmailAddress	mobilePhone	Employeenumber

$Users = Import-Csv -Path C:\BulkAddUser\BulkUsers.csv

foreach ($User in $Users) {
Get-Mailbox -Identity $User.SamAccountName
}

foreach ($User in $Users) {
# Read user data from each field in each row
# the username is used more often, so to prevent typing, save that in a variable
$Username = $User.SamAccountName

# Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username}) {
     #If user does exist, give a warning
     Write-Warning "A user account with username $Username already exist in Active Directory."
}
else {
    # User does not exist then proceed to create the new user account

    # create a hashtable for splatting the parameters
    $userProps = @{
        SamAccountName             = $User.SamAccountName                   
        Path                       = $User.Path      
        Name                       = $User.Name
        DisplayName                = $User.DisplayName
        UserPrincipalName          = $user.UserPrincipalName 
        Department                 = $User.Department
        Description                = $User.Description
        Title                      = $User.Title
        EmailAddress               = $User.Email
        AccountPassword            = (ConvertTo-SecureString $User.Password -AsPlainText -Force) 
        Enabled                    = $true
        ChangePasswordAtLogon      = $true
        PasswordNeverExpires       = $false
        MobilePhone                = $User.MPhone
        EmployeeNumber             = $User.Employeenumber
    }   #end userprops   

   New-ADUser @userProps
   Enable-Mailbox -Identity  $User.SamAccountName  -Alias $User.SamAccountName  -Database DB1602
   Add-DistributionGroupMember -Identity  qyjbdrt -Member $User.SamAccountName

  Write-Host "The user ADaccount $User is created." -ForegroundColor Green




} #end else

}

Exchange 从所有组中移除指定账号(执行有错)

第1种:Microsoft 365: PowerShell to Remove a user from All Distribution Groups – SharePoint Diary

  • $UserToRemove= "Steve@Crescent.com"
  • Try {
  •     #Connect to Exchange Online
  •     #Connect-ExchangeOnline
  •     #Get All Distribution Lists - Excluding Mail enabled security groups
  •     $DistributionGroups= Get-Distributiongroup-resultsizeunlimited |  Where {!$_.GroupType.contains("SecurityEnabled")}
  •     #Loop through each Distribution Lists
  •     ForEach($Group in $DistributionGroups)
  •     {
  •         #Check if the Distribution List contains the particular user
  •         If((Get-DistributionGroupMember$Group.Name | Select-ExpandPrimarySmtpAddress) -contains $UserToRemove)
  •         {
  •             Remove-DistributionGroupMember -Identity $Group.Name-Member $UserToRemove -Confirm:$false
  •             Write-host"Removed user from group '$Group'"-fGreen
  •         }
  •     }
  • }
  • Catch {
  •     write-host -f Red "Error:"$_.Exception.Message
  • }

第2种

$DGs= Get-DistributionGroup foreach( $dg in $DGs) { Remove-DistributionGroupMember $dg -Member “user@domain.com” }

Exchange 添加邮件规则发送离职通知

  • Get-TransportRule -State Enabled | fl *
  • Get-TransportRule -State Enabled | Set-TransportRule -SentToMemberOf ‘Ex-Employee’
  • 查看 禁用的AD 用户
  • $DisableAduser = Get-ADUser -Filter {Enabled -eq “False” } | select name
  • New-DistributionGroup -OrganizationalUnit ‘OU=离职员工,DC=abc,DC=com’ -Name ‘Ex-Employee’ -Type Security
  • Get-DistributionGroupMember -Identity Ex-Employee | fl Name
  • 查看 特定OU 用户
  • $ExEployee01 = Get-ADUser -Filter * -SearchBase “OU=离职员工,DC=abc,DC=com” | Select-Object SamAccountName
  • $ExEployee01.SamAccountName | Add-DistributionGroupMember -Identity Ex-Employee
  • 查看 分发组(通讯组)
  • Get-ADGroup -Filter {GroupCategory -eq ‘Distribution’} | select Name
  • 查看 安全组
  • Get-ADGroup -Filter {GroupCategory -eq ‘Security’} | select Name