Reputation: 25
First of all, this is my first Stack Overflow post so I apologize if this has been answered somewhere else and I couldn't find it, but here's what I'm trying to figure out.
I'm looping through a CSV list of usernames, getting the Office 365 license status for each user, and I want to write a string back to the CSV for that user based on the license status. So for example, the CSV looks like this:
Username License1 License2
user1
user2
user3
And here's the code:
$users = Import-Csv .\users.csv
foreach ($user in $users) {
$licenses = (Get-MsolUser -UserPrincipalName $_.Username).Licenses.AccountSkuId
if ($licenses -match "XYZ") {
$_License1 = "XYZ" | Export-Csv .\users.csv -NoTypeInformation
}
}
So if the first two users have the "XYZ" license then the CSV would be updated to look like this:
Username License1 License2
user1 XYZ
user2 XYZ
user3
It's not working, and I'm sure the problem has to do with how I'm trying to export the data back to the original CSV, but I don't know how to do it the right way. Any help is greatly appreciated, and if there is a better way to do this I'm all ears.
Thanks in advance!
Upvotes: 2
Views: 326
Reputation: 32180
Try something like this:
$users = Import-Csv .\users.csv
foreach ($user in $users) {
$licenses = (Get-MsolUser -UserPrincipalName $_.Username).Licenses.AccountSkuId
if ($licenses -match "XYZ") {
$user.License1 = "XYZ"
}
}
$users | Export-Csv .\users.csv -NoTypeInformation
Upvotes: 2