SOLVED

Powershell bulk creation - error with UserPrincipalName

Brass Contributor

Hi

 

I use a simple script (and it works) to bulk create new users on my M365 tenant (cloud based only).

 

  1. When I set the UserPrincipalName to e.g. [jan . jensen @ DOMAIN . onmicrosoft . com] it works (disregard the spaces, if I didn't use spaces in the example, the email gets removed by the editor).

 

 

I'm missing something here, because, I assumed that the UserPrincipalName also could be the new email address. 

 

What do I need to in order to also create a valid email address as well?

 

Below is the online script, and the CSV file it references.

 

 

 

 

 

 

Import-Csv -Path "C:\Users\Jens Jakobsen\users.csv" | ForEach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path "C:\Users\Jens Jakobsen\Results.csv"

 

 

 

 

 

 

 

Thanks.

7 Replies
What error do you receive when creating a test user manually?

New-MsolUser -DisplayName 'Harm Test' -FirstName Harm -LastName Test -UserPrincipalName Email address removed -UsageLocation NL

Because this should work if you have the domain added to your tenant of course ;) (I just used the above syntax and my user is there)

Hello Harm

The example email you used has been removed by the editor. You have to use the syntax, as I did in my first question.

I tried a one-liner, with no CSV file, and it worked like a charm. Below is the one-liner I used:

New-MsolUser -DisplayName "Lars Larsen" -FirstName Lars -LastName Larsen -UserPrincipalNamelars . larsen @ DOMAIN . dk -UsageLocation DK -LicenseAssignment kvikkastrup:STANDARDPACK

Yes, it removes it and I should have used that syntax :) But ok, if you run it like that it works.. But why doesn't it work when importing it from a csv-file, syntax seems correct. Any error messages when trying to create an account using csv?

@Harm_Veenstra 

I used the script:

Import-Csv -Path "C:\Users\Jens Jakobsen\users.csv" | ForEach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path "C:\Users\Jens Jakobsen\Results.csv"

I received the error:

New-MsolUser : You must provide a required property: Parameter name: UserPrincipalName
At line:1 char:64
+ ...  | ForEach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.Fi ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [New-MsolUser], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.RequiredPropertyNotSetException,Microsoft.Online.Administration.Automation.NewUser

New-MsolUser : You must provide a required property: Parameter name: UserPrincipalName
At line:1 char:64
+ ...  | ForEach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.Fi ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [New-MsolUser], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.RequiredPropertyNotSetException,Microsoft.Online.Administration.Automation.NewUser

The domain has been accepted (see screendump). 

 

It's really weird, and any help is welcome.

 

Thanks.

best response confirmed by jensjakobsen1966 (Brass Contributor)
Solution

@jensjakobsen1966 The delimeter parameter was not specified and import-csv expects the comma and the delimeter is a semicolon in your sample file. Added the parameter and it should work now :p 

Import-Csv -Path "C:\Users\Jens Jakobsen\users.csv" -Delimeter ';' | ForEach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path "C:\Users\Jens Jakobsen\Results.csv"

 

Thanks, the change from semicolon to commas worked. Much appreciated - I felt blind at the end :)
No problem, didn't see it until I downloaded your sample file and didn't get the correct output from just doing a import-csv. Sometimes you have to chop the script into pieces and validate each one ;)
1 best response

Accepted Solutions
best response confirmed by jensjakobsen1966 (Brass Contributor)
Solution

@jensjakobsen1966 The delimeter parameter was not specified and import-csv expects the comma and the delimeter is a semicolon in your sample file. Added the parameter and it should work now :p 

Import-Csv -Path "C:\Users\Jens Jakobsen\users.csv" -Delimeter ';' | ForEach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path "C:\Users\Jens Jakobsen\Results.csv"

 

View solution in original post