Use PowerShell to export user information from users in Exchange to Office 365

Source: Internet
Author: User

Today to introduce an article on PowerShell, in general, if we want to migrate Exchange to Office 365, there are a number of ways, including Microsoft's own and third-party, if we want to migrate through third-party tools, can be through the AAD Connect to synchronize user data to Office 365, of course, this approach requires some additional action in order for the mailbox to be created, and if not via AAD Connect, we can also directly export some of the information in exchange from the server, It is then created directly in Office 365 in CSV mode.


Today to share a user's information from the Office 365 from the script, run very simple, the bottom of the code to share

param  ([Parameter (valuefrompipeline =  $true)] $OUPath, [string] $DomainName, [string]$ Foro365exportto = [environment]::getfolderpath ("Desktop")  +  "\"  +  " Userinfoforo365fromexchange.csv ") try{$Error. Clear () import-module activedirectory -erroraction  ' Stop ' $name  =  (get-wmiobject -class win32_computersystem) .name +  "."  +  (Get-wmiobject -class win32_computersystem). domain$pssessionadded = $ falseget-pssession | %{if  ($_. computername -eq  $name) {$PsSessionAdded  =  $true}}if  ($PsSessionAdded  -eq $ False) {$Session  = new-pssession -configurationname microsoft.exchange -connectionuri   ("HTTP//"  +  $name  +  "/powershell/")  -authentication kerberos - erroraction  ' Stop ' import-pssession  $Session  -ErrorAction  ' Stop '  | out-null}} catch{throw  $Error [0]. Exception.message}function test-oupath () {param  ([string] $path) $OUExists  = [adsi]::exists (" ldap://$path ") return  $OUExists}if  ([String]::isnullorempty ($DomainName)  -or  ([string]::i Snullorwhitespace ($DomainName))) {throw  "$ (get-date)  * Please provide your  Domain name "}if  ($OUPath) {#Get  Mailbox with OUPathif  (test-oupath  $OUPath) { $Mailbox  = Get-Mailbox -Filter { RecipientTypeDetails -eq  "Usermailbox"  } -ResultSize Unlimited -OrganizationalUnit  $OUPath}else{write-warning  "$ ( get-date)  *  $OUPath  does not exist, please check "exit}}else{#Get  all  Mailboxes$Mailbox = Get-Mailbox -Filter { RecipientTypeDetails -eq  " Usermailbox " } -resultsize unlimited}[pscustomobject[]" $O 365userobjects =  $nullif   ($OUPath) {WRITE-HOST  " $ (get-date)  * The OU is  $OUPath,  begin to collect information,  please wait "}else{write-host " $ (get-date)  * no ou provided, begin  to collect information of all mailboxes, please wait "} $Mailbox  | %{#============================================================================ #For  o365  user provision$user = get-aduser -identity $_. samaccountname -properties * $UserName  =  $User. userprincipalname$username = $ Username.split ("@") [0] $UserName  =  $UserName  +  "@"  +  $DomainName $firstname =   $User .givenname$lastname =  $User .surname$displayname =  $User. Displayname$jobtitle  =  $User .title$department =  $User .department$officenumber =  "" $OfficePhone   =  $User .officephone$mobilephone =  $User. Mobilephone$fax =  $User .fax$address =  $User .streetaddress$city =  $User. City$State  =  $User .state$zipcode =  $User .postalcode$country =  $User. country#============== ============================================================== $O 365userobject = new-object - Typename psobject$o365userobject | add-member -membertype noteproperty -name   ' User name '  -Value  $UserName $o365userobject | add-member -membertype  noteproperty -name  ' first name '  -Value  $FirstName $o365userobject |  add-member -membertype noteproperty -name  ' Last name '  -Value  $LastName $ o365userobject | add-member -membertype noteproperty -name  ' Display Name '  -Value  $DisplayName $o365userobject | add-member -membertype noteproperty - name  ' Job title '  -value  $JobTitle $o365userobject | add-member -membertype noteproperty -name  ' Department '  -Value  $Department $o365userobject | add-member -membertype  noteproperty -name  ' Office number '  -Value  $OfficeNumber $o365userobject |  add-member -membertype noteproperty -name  ' Office phone '  -Value $ officephone$o365userobject | add-member -membertype noteproperty -name  ' Mobile  phone '  -Value  $MobilePhone $o365userobject | add-member -membertype  noteproperty -name  ' Fax '  -Value  $Fax $o365userobject | add-member - membertype noteproperty -name  ' Address '  -Value  $Address $o365userobject |  add-member -membertype noteproperty -name  ' city '  -Value  $City $o365userobject  | add-member -membertype noteproperty -name  ' state or province '  -Value  $State $o365userobject |  add-member -membertype noteproperty -name  ' Zip or postal code '  -Value   $ZipCode $o365userobject | add-member -membertype noteproperty -name  ' Country or region '  -Value  $Country $o365userobjects +=  $O 365userobject}if  ($ o365userobjects -ne  $null) {try{$Error. Clear () $O 365userobjects | export-csv - notypeinformation -append -literalpath  $ForO 365exportto -force -erroraction  ' Stop ' write-host   ' $ (get-date)  * Done. Please check  $ForO 365ExportTo "}catch{ write-warning  $Error [0]. exception.message}}else{write-warning  "$ (get-date)  * something wrong, didn ' T get  any info "}



Run method is not introduced, the parameters are very few, you can also specify the exported OU

Use PowerShell to export user information from users in Exchange to Office 365

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: info-contact@alibabacloud.com 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.