Link to home
Start Free TrialLog in
Avatar of Christian Hans
Christian HansFlag 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.
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

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

Avatar of 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
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ...
Might be best if you simply give us example of the file :)
Avatar of oBdA
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 $_ ...
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