export objectid of users listed in *.csv file

kitsbee 21 Reputation points
2020-08-05T22:19:13.52+00:00

Hi ,

trying to extract objectid for list of users from csv. Csv file contains header with UPN . This does not seem to be working.

import-csv -path "C:\test1.csv" |
foreach {
Get-Msoluser -Userprinicpalname $_.UPN | Select-Object -Property ObjectID
} | Export-CSV -Path "C:\test9.csv"

Get-MsolUser : A parameter cannot be found that matches parameter name 'Userprinicpalname'.

Can you advise do i need to change the header. what valueUserprinicpalname'. is looking ?

Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2020-08-05T22:56:55.123+00:00

    Well, there isn't a parameter named "-Name". That's pretty clear in both the error message and the help text for the cmdlet.

    You can use the -UserPrincipalName, or the -SearchString parameters. Or you can use something like this:

    import-csv -path C:\test1.csv |
        ForEach-Object{
            $Name = $_.Name
            Get-MsolUser -All |
                Where-Object {$_.Name -eq $Name} |
                    Select-Object @{n=Name;e={$Name}},@{n=ObjectID;e={$_.ObjectID}}
        } | Export-CSV --Path C:\test9.csv -NoTypeInformation
    

2 additional answers

Sort by: Most helpful
  1. AmanpreetSingh-MSFT 56,871 Reputation points Moderator
    2020-08-06T04:55:11.607+00:00

    Hi @kitsbee

    Please find below the script that you can use for this purpose:

    $users = Import-Csv -Path C:\test1.csv  
    $loop = foreach ($user in $users) {  
    Get-MsolUser -UserPrincipalName $user.upn | Select-Object UserPrincipalName, ObjectID  
    }   
    $loop | Export-CSV -Path C:\test9.csv -NoTypeInformation  
    

    Below is how my input file test1.csv looks like:

    16021-image.png

    -----------------------------------------------------------------------------------------------------------

    Please "Accept the answer" if you find the information helpful. This will help us and others in the community as well.

    1 person found this answer helpful.

  2. Alfredo Revilla - Upwork Top Talent | IAM SWE SWA 27,526 Reputation points Moderator
    2020-08-06T04:28:35.483+00:00

    UserPrincipalName is mispelled (Userprinicpalname). Also, it's recommended to use the (newer) AzureAD powershell module:

       Get-AzureADUser -Filter "userPrincipalName eq '$_.UPN'"  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.