Foreach loop invalid value

Hi all,

Just wanted to check what i am missing. I have imported a CSV file that has 2 headers. UserPrincipalName and UsageLocation.

foreach ($user in $users) { Set-MsolUser -UserPrincipalName $user.userprincipalname -UsageLocation $user.UsageLocation }

it gives me the error below
Set-MsolUser : Invalid value for parameter. Parameter Name: UsageLocation.

Looking at other documentation doing
Set-MsolUser -UserPrincipalName user@domain.com -UsageLocation “FR” should work. so why does mine not? difference for me is that is it coming from a CSV file and going through a foreach loop. No sure what knowledge i am missing here. please advice.

As a debugging step, I would usually display the contents of $user as I go, so I can verify what’s in the variable before relying on the values.

Hi Don,

So i actually managed to get it working.

$Users = Import-Csv -Path $ImportFile
    Foreach ($User in $Users) {
        $UPN = $User.UserPrincipalName
        $UsageLocation = $User.UsageLocation
        Write-Verbose "Setting Usage Location to $UsageLocation on $UPN."
        Set-MsolUser -UserPrincipalName $UPN -UsageLocation $UsageLocation
    }

One question though i do have. what is the difference in using Set-MsolUser -UserPrincipalName $User.UserPrincipalName or Set-MsolUser -UserPrincipalName $UPN

Assuming you have done something like $UPN = $User.UserPrincipalName first.

Should there be a difference? thats what i am trying to work out.

I’ve encountered what you are experiencing above - try doing $($user.userprincipalname) and $($user.usagelocation) and you should get it to work without having to assign it to another variable. Assigning the values to another variable in something like this is not a big issue - but in larger scripts that adds up to time and memory.

Thank you Paul. I do wish a had a bit better understanding of why doing $($var.something) works better.

Using $($var.value) is a subexpression. It allows you to use the value of a property without having to do the extra step like you were doing. What you were doing is not invalid at all. It works on smaller scripts typically without any impact - but on larger scripts/data sets there tends to be an increase in the amount of memory used to keep that variable up to date and continually changing instead of using the value of the object which you already have in the script. In working with ad objects I’ve seen a script that used assigning values of an object’s property to variables to cause a citrix server to freeze up and kick everyone off of it due to the amount of ram it was using (it was using about 12gb just for that one powershell process compared to the 16gb available). Modifying that script to use sub expressions - that script ran way faster and only the powershell process only ended up taking about 100mb of ram.

A few quick sites on the sub expression are below to hopefully help ya out. Good luck on your future scriptings!

https://blogs.technet.microsoft.com/stefan_stranger/2013/09/25/powershell-sub-expressions/