Bulk assigning new SMTP addresses & new company name with powershell based on csv file

Viewed 523

I have to create a Powershell script that assigns a new main SMTP address to users of a csv file. They also need some other addresses. Besides that, those users need to change their company name. I am really new to all that stuff and tried to cobble some stuff together which wouldn´t work and it´s driving me crazy. :(

If you need more info please tell me. :)

Here is the code I´ve tried so far:

$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Connect-AzureAD -Confirm

#Assigning addresses & company
Import-CSV "C:\CSVPowershell\userstest.csv" | ForEach {
Set-Mailbox $_.Identity -EmailAddresses @{add= $_.MainEmailAddress}
Set-AzureADUser -ObjectId $_.Identity -CompanyName $_.Company
}


#close
Remove-PSSession $Session
1 Answers

Please check the below workaround:

CSV file

UserName, Mailform1, Mailform2 
aaa test, atest@companyname.com , atest@companyname_Dept.com
bbb test, btest@companyname.com , btest@companyname_Dept.com
ccc test, ctest@companyname.com , ctest@companyname_Dept.com

PowerShell Script

Note: In your code, you are missing the operation add. Either you have to use Add / Remove in your script to perform the related operation.

    Import-CSV "C:\Users\Admin\UserEmailAddress.csv" | ForEach 
    {
        # Changing the Main Email address into your required Email.
        #Adding Multiple Email addresses (here 2 Email addresses)
        Set-Mailbox $_.UserName -EmailAddresses @{add= $_.Mailform1, $_.Mailform2}
    }

Suppose you are trying to add only one SMTP address to User follow the below:

CSV File

UserName, Mailform1
aaa test, atest@companyname.com 
bbb test, btest@companyname.com 
ccc test, ctest@companyname.com 

PowerShell Script

    Import-CSV "C:\Users\Admin\UserEmailAddress.csv" | ForEach 
    {
        # Changing the Main Email address into your required Email.
        
        Set-Mailbox $_.UserName -EmailAddresses @{add= $_.Mailform1}
    }

Refer here for more information

Related