'Powershell: ForEach + GetMsol loop

I had tried to take a look on videos, went thru articles in my 2 books but still cannot nail it :-/ Goal of my script: to get the UPN's based on DisplayName of user's accounts.

My CSV example:

  • DisplayName
  • user1
  • user2
  • user3

I am successfull when I just run:

Get-MsolUser -SearchString 'user1' | Select-Object UserPrincipalName

Importing all displaynames to array:

$userlist = Import-Csv .\ul.csv

Check I did import correctly:

$userlist

Sadly I'm failing here: to run the loop thru the imported CSV "objects" :-/

I tried 1:

$userlist.foreach(
            {
                Get-MsolUser -SearchString 'DisplayName' | Select-Object UserPrincipalName
            })

I tried 2:

foreach ($user in $userlist)
{
    Get-MsolUser -Searchstring DiplayName | Select-Object UserPrincipalName 
}

I tried 3:

$userlist | ForEach-Object {Get-MsolUser -Searchstring DisplayName | Select-Object UserPrincipalName}

None of them worked :-(



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source