SimonS
SimonS

Reputation: 1973

Powershell & Office365: Get each Item from Hashtable for Set-msoluserlicense

I'm trying to write a Script which changes every License from each Office365 User we have. What annoys me here is that you have to use the "UserPrincipalName" Option for the Set-MsOlUserLicense Command. So I'm trying to cheat with hashtables

That's what I have so far:

Import-module msonline
$cred = Get-Credential
connect-msolservice -credential $cred
$UserPrincipal = @{}
Import-csv Z:\Powershell-Scripts\PS-lists\MSOLUserPrincipalNames.csv | ForEach-Object {Set-msolUserLicense -UserPrincipalName $UserPrincipal.Get_Item() -AddLicenses "syndication-account:ENTERPRISEPACK" -RemoveLicense "syndication-account:STANDARDPACK"}

The -UserPrincipalName.GetItem() won't work. how can I get each value from the hashtable into this command?

Sorry for rusty english, I hope the question is understandeable :)

Upvotes: 1

Views: 255

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174825

Assuming that MSOLUserPrincipalNames.csv contain one column with the header UserPrincipalName, you can refer to that property's value with $_.UserPrincipalName inside the ForEach-Object scriptblock:

Import-Csv Z:\Powershell-Scripts\PS-lists\MSOLUserPrincipalNames.csv | ForEach-Object {
    Set-msolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses "syndication-account:ENTERPRISEPACK" -RemoveLicense "syndication-account:STANDARDPACK"
}

The $_ variable, also known as $PSItem, always refers to the current item in the pipeline

Upvotes: 1

Related Questions