Skip to content

Commit 4a72ba2

Browse files
authored
Add Export of New User Details with License Information
This update enhances the script by exporting detailed information about newly created users, including their license details, to a CSV file. Previously, the script only exported the original data from the imported `NewAccounts.csv` file. The new functionality ensures that the output includes: - User Principal Name - Display Name - Assigned License Details The results are saved to `C:\temp\NewAccountResults.csv` for further review or auditing. This change improves traceability and provides a comprehensive overview of the newly created accounts.
1 parent f4f0ba0 commit 4a72ba2

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

microsoft-365/enterprise/create-user-accounts-with-microsoft-365-powershell.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ New-MgUser -DisplayName "John Doe" -GivenName "John" -Surname "Doe" -UserPrincip
103103
# Create a password profile
104104
$PasswordProfile = @{
105105
Password = 'Password123'
106-
}
106+
}
107+
108+
# Create a table for the new users results
109+
$results = @()
107110
108111
# Loop through each user in the CSV file
109112
foreach ($user in $users) {
@@ -113,10 +116,20 @@ New-MgUser -DisplayName "John Doe" -GivenName "John" -Surname "Doe" -UserPrincip
113116
# Assign a license to the new user
114117
$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
115118
Set-MgUserLicense -UserId $newUser.Id -AddLicenses @{SkuId = $e5Sku.SkuId} -RemoveLicenses @()
119+
120+
# Get the new user's details
121+
$newUser = Get-MgUser -UserId $newUser.Id -Property UserPrincipalName, DisplayName, LicenseDetails
122+
123+
# Add the new user results to the results table
124+
$results += [PSCustomObject]@{
125+
"User Principal Name" = $newUser.UserPrincipalName
126+
"Display Name" = $newUser.DisplayName
127+
"License Details" = $newUser.LicenseDetails | ForEach-Object { $_.SkuPartNumber } -join ', '
128+
}
116129
}
117130
118131
# Export the results to a CSV file
119-
$users | Export-Csv -Path "C:\temp\NewAccountResults.csv" -NoTypeInformation
132+
$results | Export-Csv -Path "C:\temp\NewAccountResults.csv" -NoTypeInformation
120133
```
121134
122135
3. Review the output file to see the results.

0 commit comments

Comments
 (0)