Hi;
I have a list (csv file) of over 100 users. My csv file has the following columns:
UserPrincipalName, LastName, FirstName, DisplayName
Some of the users in the csv file have accounts in Office 365 and some do not. I would like to use Powershell to go through the list. If a user has an Office 365 account, then Powershell will display a message stating that the user already exists
in Office 365. If the user does not have an Office 365 Account, Powershell will create an account for that user. Here is what I’m thinking (I’ve already connected to Office 365 and MSOL services in Powershell):
$Accounts = Import-csv ..\*.csv
ForEach ($account in $accounts)
{
$AccountExists = Get-MsolUser -UserPrincipalName $account.UserPrincipalName
If ($AccountExists -ne $Null) {Write-host "$($Account.Displayname) exists in Office 365"}
Else {New-MsolUser -UserPrincipalName $Account.UserPrincipalName, -LastName $Account.LastName, -FirstName $Account.FirstName, -DisplayName $Account.Displayname}
}
When I run this, I get the following errors and it has to do with creating the new user:
Get-MsolUser : User Not Found. User: XXXX@XXX.com
At C:\PS_Scripts\test11512.ps1:4 char:30
+ $AccountExists = Get-MsolUser <<<< -UserPrincipalName $account.UserPrincipal
Name
+ CategoryInfo : OperationStopped: (:) [Get-MsolUser], MicrosoftO
nlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserN
otFoundException,Microsoft.Online.Administration.Automation.GetUser
How do not show this error message from the output ?
Regards