Forum Discussion
Charles Willwerth
Oct 12, 2016Brass Contributor
Powershell to create user AND update Work E-mail in User Profile
Hello, I am trying to build a powershell script that will run against SharePoint Online. It will create a new user, and update their Work-Email property in their SharePoint Online User Profile. T...
Oct 12, 2016
What you need to take into account is that you first have created the user in Azure AD and the object that is created behind the scenes is propagated first to SPO DS (that's why you are able to use the user on a team site) what does not mean the same happens with user profiles...you need to wait until the new profile is available there (sorry but I don't recall now the schedule for user profiles in SPO)
Charles Willwerth
Oct 12, 2016Brass Contributor
Hmm... I'm not sure that it's "scheduled." It always works once I've logged in, which tells me there's a way to trigger it. Also, I've waited over a weekend once, and the profile was still not available.
- SanthoshB1Oct 13, 2016Bronze ContributorIn order to make the profile available in SPO the user need to access the shared files in that site. Alternately you can use the cmd Add-SPOUser to add the user in any of the sites and then run your command. HTH.
- Charles WillwerthNov 30, 2016Brass Contributor
I did complete this, but I'm not happy with how it works. First, as background, the users I'm creating in this case are also added to Azure AD Groups that have access to SharePoint Online, so it may be that SharePoint is creating user profiles for them as a result of this. Still, I have to wait for SPOnline to do this. I tried the "New-SPOUser" command line to no avail.
I essentially have a "While" loop that tries to modify the property, and catches the error if it occurs so that it can try again in 30 seconds:$test = 1 While ($test -eq 1) { Try { Write-Host "Trying to set property" Set-SPOUserProfileProperty -Account $userprincipalname -PropertyName "Workemail" -Value $NewUserEmail -ErrorAction Stop $test = 0 $curTime = Get-Date } Catch { $badTime = Get-Date Write-Host $_.Exception.Message -ForegroundColor Red Write-Host "Didn't work at $badTime, trying again in 30 seconds. This usually takes up to 30 minutes." Start-Sleep -Seconds 30 } }