Create a new ad account, mailbox, and PowerShell script to add a distribution group

Source: Internet
Author: User

According to the company's needs, wrote a new ad account, mailbox, add a distribution group PowerShell script, first need to edit the CSV file, the format is as follows:

650) this.width=650; "src=" "title=" 1.jpg " alt= "Wkiol1xxn9gbm92aaacgfom93ka712.jpg"/>

The code is as follows, added to the group delay of 6 seconds to run, failed to reproduce the attempt 4 times after the function:

import-module activedirectory$erroractionpreference =  "Silentlycontinue" #判断是否有重名账号存在 $f  =   $FalseImport-csv -path d:\newuser\info.csv | foreach {  $a  =  Get-aduser -identity $_.accountif ($a  -ne  $null) {echo  ($_.account+ ' account already exists in AD. ') $f  =  $true}else{ } $a  =  $null}if ($f  -eq  $true) {echo  Please modify the above username in the CSV file. Rerun the script. cmd /c pauseexit}else{echo  does not have the name of the username, loading the Exchange module and creating a new user account, please wait ... } #新建账号和邮箱 $erroractionpreference  =  "Continue" add-pssnapin$password = convertto-securestring -asplaintext  6yhn^YHN -ForceImport-Csv -Path D:\newuser\Info.csv | foreach {   New-Mailbox -Name $ -Alias $_.account -OrganizationalUnit  ' /teset/newuser '  -UserPrincipalName  ($_.account+ ' @teset. com ')  -samaccountname $_.account -displayname $ -password  $password  -Database  ' User04 '  -ResetPasswordOnNextLogon  $true} $erroractionpreference  =  "Silentlycontinue" #添加到相应的组choice  /t 6 /d y /n | out-nullimport-csv -path d:\newuser\info.csv | foreach {$b  = 1$b =  add-adprincipalgroupmembership -identity $_.account -memberof shofficeif ($b  -eq   $null) {echo  ($_.account+ ' join Shoffice Group success ')}else{$b  = add-adprincipalgroupmembership - Identity $_.account -memberof shofficeif ($b  -eq  $null) {echo  ($_.account+ ' Join Shoffice Group Success ')}else{$b  = add-adprincipalgroupmembership -identity $_.account - Memberof shofficeif ($b  -eq  $null) {echo  ($_.account+ ' join Shoffice Group success ')}else{$b  =  Add-adprincipalgroupmembership -identity $_.account -memberof shofficeif ($b  -eq  $null) {echo  ($_.account+ ' join Shoffice Group Success ')}else{echo  ($_.account+ ' join Shoffice Group failed!!) ')}}}} $c  = 2$c = add-adprincipalgroupmembership -identity $_.account - Memberof $_.group1if ($c  -eq  $null) {echo  ($_.account+ ' join ' +$_.group1+ ' group success ')}else{$c  =  add-adprincipalgroupmembership -identity $_.account -memberof $_.group1if ($c  - eq  $null) {echo  ($_.account+ ' join ' +$_.group1+ ' group success ')}else{$c  = add-adprincipalgroupmembership  -identity $_.account -memberof $_.group1if ($c  -eq  $null) {echo  ($_.account+ ' Join ' +$_.group1+ ' group success ')}else{$c  = add-adprincipalgroupmembership -identity $_.account - Memberof $_.group1if ($c  -eq  $null) {echo  ($_.account+ ' join ' +$_.group1+ ' group Success ')}else{echo  ( $_.account+ ' join ' +$_.group1+ ' group failed! ')}}} cmd /c  "Pause"

After successful execution:

650) this.width=650; "src=" "title=" 2.jpg " alt= "Wkiom1xxob2h--yfaafdmt_vrhi305.jpg"/>

Have a question, give me a message, discuss together.

This article is from the "Wings of God" blog, so be sure to keep this source

Create a new ad account, mailbox, and PowerShell script to add a distribution group

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.