Avatar of Christian Hans
Christian Hans
Flag for United States of America asked on

O365: Get-Content | Get-MsolUser Information

I am looking to run a PowerShell command to export specific data from my Office 365 Tenant...

Referencing 'O365Users.txt' contains a list of UPNs.

Get-Content C:\TEMP\O365Users.txt | Get-MsolUser -ALL | Select UserPrincipalName,DisplayName,Title,UsageLocation,Office,Department,IsLicensed,SignInName | Sort Title | Export-CSV C:\TEMP\Titles.csv

Error:
Get-MsolUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

How could I pull this specific data? Thanks in advance.
Microsoft 365PowershellExchange

Avatar of undefined
Last Comment
Christian Hans

8/22/2022 - Mon
Vasil Michev (MVP)

Is it a CSV file? If so, try the following:

Import-CSV C:\TEMP\O365Users.txt | % { Get-MsolUser -UserPrincipalName $_.UPN | Select UserPrincipalName,DisplayName,Title,UsageLocation,Office,Department,IsLicensed,SignInName | Sort Title } | Export-CSV C:\TEMP\Titles.csv

Open in new window

Christian Hans

ASKER
The file I am pulling from is a TXT file, so even when I create a CSV to pull from I get the following:

Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Christian Hans

ASKER
Import-CSV C:\TEMP\O365Users.txt Results:

Import-CSV C:\TEMP\O365Users.txt | % { Get-MsolUser -UserPrincipalName $_.UPN | Select UserPrincipalName,DisplayName,Title,UsageLocation,Office,Department,IsLicensed,SignInName | Sort Title } | Export-CSV C:\TEMP\Titles.csv

Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
At line:1 char:98
+ ... ng\O365Users.txt | % { Get-MsolUser -UserPrincipalName $_.UPN | Selec ...


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

Import-CSV C:\TEMP\O365Users.csv Results:

Import-CSV C:\TEMP\O365Users.csv | % { Get-MsolUser -UserPrincipalName $_.UPN | Select UserPrincipalName,DisplayName,Title,UsageLocation,Office,Department,IsLicensed,SignInName | Sort Title } | Export-CSV C:\TEMP\Titles.csv

Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
At line:1 char:98
+ ... ng\O365Users.csv | % { Get-MsolUser -UserPrincipalName $_.UPN | Selec ...

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

Get-Content C:\TEMP\O365Users.txt Results:

Get-Content C:\TEMP\O365Users.txt | % { Get-MsolUser -UserPrincipalName $_.UPN | Select UserPrincipalName,DisplayName,Title,UsageLocation,Office,Department,IsLicensed,SignInName | Sort Title } | Export-CSV C:\TEMP\Titles.csv

Get-MsolUser : Cannot bind argument to parameter 'UserPrincipalName' because it is null.
At line:1 char:99
+ ... ng\O365Users.txt | % { Get-MsolUser -UserPrincipalName $_.UPN | Selec ...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Vasil Michev (MVP)

Might be best if you simply give us example of the file :)
oBdA

1. Import-CSV C:\TEMP\O365Users.txt
Like with my first example above (https:#a41768991 which would be the easiest to use, actually), you can import a plain text file (without headers) as csv if you pass the header list to Import-Csv; in your example above, that would be "UPN".
Import-CSV C:\TEMP\O365Users.txt -Header UPN | % { Get-MsolUser -UserPrincipalName $_.UPN ...

2. Import-CSV C:\TEMP\O365Users.csv
If you're importing from a csv and working with the ForEach cmdlet,, the csv needs a header line, and you need to pass the loop variable with the column name you used as the header line. In your second example above, that would be 'UPN':
... -UserPrincipalName $_.UPN ...
The csv file would need to look like that:
UPN
john.doe@domain.com

3. Get-Content C:\TEMP\O365Users.txt
If you're importing from a plain text file, and working with the ForEach cmdlet, you only need to pass the loop variable; the UPN property of a string will always be $Null:
... -UserPrincipalName $_ ...
Christian Hans

ASKER
That worked flawlessly... thank you oBdA

Get-Content C:\TEMP\O365Users.txt | % {Get-MsolUser -UserPrincipalName $_ | Select UserPrincipalName,DisplayName,Title,UsageLocation,Office,Department,IsLicensed,SignInName | Sort Title} | Export-CSV C:\TEMP\Titles.csv
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.