none
Powershell Issue - foreach {$username in $accounts} RRS feed

  • Question

  • Hello,

    $accounts = import-csv F:\path\file.txt

    foreach ($username in $accounts) {write-host $username}

    Obviously very basic and I am wanting to use the output to connect to the Office365 cloud and query ImmutableID's, however whenever I do this my results end @{Username=abcd1234}

    What am I obviously overlooking here?

    In the long run this is what I would like to achieve.

    Connect-MsolService –Credential $cred
    $accounts = import-csv F:\path\file.txt 
    foreach ($username in $accounts){
    Get-Msoluser -UserPrincipalName "$username@domain.com" | Select ImmutableID}

    Basically to list anyone in this spreadsheet weather they have an immutableid or not.

    When I run this, it also states regarding could not find @{Username=abcd1234@domain.com

    Many Thanks

    • Edited by Tarrley Tuesday, August 19, 2014 8:40 AM more info
    Tuesday, August 19, 2014 8:23 AM

Answers

  • What if you change it slightly to this - 

    $accounts = import-csv F:\path\file.txt 
    foreach ($username in $accounts){
    $UPN = $username.username + '@domain.com'
    Get-Msoluser -UserPrincipalName $UPN | Select ImmutableID}

    • Marked as answer by Tarrley Thursday, August 21, 2014 9:00 AM
    Tuesday, August 19, 2014 9:11 AM

All replies

  • What is the header for that column in your CSV file? You will need to use $username.columheader to get just the username.
    Tuesday, August 19, 2014 8:47 AM
  • That's good, however i think it's the @ that's causing me issues.

    The column header is username, so if I run foreach ($username in $accounts) {write-host $username.username"@domain.com"} it does do what I want, however add's a space between the username and the @domain.com...

    If I adjust the " it simply does not work...

    Tuesday, August 19, 2014 9:02 AM
  • What if you change it slightly to this - 

    $accounts = import-csv F:\path\file.txt 
    foreach ($username in $accounts){
    $UPN = $username.username + '@domain.com'
    Get-Msoluser -UserPrincipalName $UPN | Select ImmutableID}

    • Marked as answer by Tarrley Thursday, August 21, 2014 9:00 AM
    Tuesday, August 19, 2014 9:11 AM