locked
Office 365 Bulk User Creation RRS feed

  • Question

  • I'm having an issue with importing a spreadsheet to create users. Specifically powershell is only creating the last line 2x but I'm not sure why. When I import the spreadsheet I see the users are being pulled in but it only appears that the last line is being used to try to create the user. 
    $users = Import-Csv -path C:\Users\username\documents\SavoUsers.csv
    
    $users | ForEach {
    $FirstName = $_.FirstName
    $LastName = $_.LastName
    $DisplayName = "$FirstName $LastName"
    $title = $_.Title
    $UserPrincipalName = $FirstName.ToLower().substring(0,1) + $LastName.ToLower() + "@company.com"  
    }
    
    
    Foreach($user in $users) {New-MsolUser -FirstName $FirstName `
       -DisplayName $DisplayName -LastName $LastName `
       -UserPrincipalName $UserPrincipalName `
       -Title $title `
       -Password "Testing123!" `
       -UsageLocation US `
       -LicenseAssignment "XXXXX:ENTERPRISEPACK", "XXXXX:INTUNE_A"
       }
    
    Remove-PSSession $Session

    Friday, April 27, 2018 6:13 PM

Answers

  • You cannot use two loops.  All of your code must be in one loop.


    \_(ツ)_/

    • Marked as answer by chendley Friday, April 27, 2018 7:57 PM
    Friday, April 27, 2018 6:19 PM

All replies

  • You cannot use two loops.  All of your code must be in one loop.


    \_(ツ)_/

    • Marked as answer by chendley Friday, April 27, 2018 7:57 PM
    Friday, April 27, 2018 6:19 PM
  • Thank you that helped. 

    Solved it 

    Import-Csv -path C:\Users\username\documents\Users.csv | ForEach {
    $FirstName = $_.FirstName
    $LastName = $_.LastName
    $DisplayName = "$FirstName $LastName"
    $title = $_.Title
    $UserPrincipalName = $FirstName.ToLower().substring(0,1) + $LastName.ToLower() + "@xxxxxx.com"  
    
    New-MsolUser -FirstName $FirstName `
       -DisplayName $DisplayName -LastName $LastName `
       -UserPrincipalName $UserPrincipalName `
       -Title $_title `
       -Password "Testing123!" `
       -UsageLocation US `
       -LicenseAssignment "xxxxxx:ENTERPRISEPACK", "xxxxx:INTUNE_A"
     }
    
     Remove-PSSession $Session

    Friday, April 27, 2018 7:57 PM