2

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!

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.