SOLVED

Get-MsolUser filtered variable

%3CLINGO-SUB%20id%3D%22lingo-sub-353898%22%20slang%3D%22en-US%22%3EGet-MsolUser%20filtered%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-353898%22%20slang%3D%22en-US%22%3E%3CP%3EWonder%20if%20anyone%20can%20help...%3C%2FP%3E%3CP%3E%26gt%3B%20I%20have%20a%20txt%20file%20containing%20a%20list%20of%20user%20principal%20names.%26nbsp%3B%3C%2FP%3E%3CP%3E%26gt%3B%20I%20need%20to%20return%20all%20users%20if%20they%20have%20a%20licence%20and%20their%20UPN%20isn't%20listed%20in%20the%20TXT%20file.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23User%20Import%23%3CBR%20%2F%3E%24Users%20%3D%20Get-Content%20C%3A%5CUsers%5CNAME%5CUserImport.txt%3C%2FP%3E%3CP%3E%23Get%20All%20Licensed%20Users%20%26amp%3B%20Filtered%3CBR%20%2F%3Eforeach%20(%24Users%20in%20%24MSOLUsers)%7B%3CBR%20%2F%3E%24User%20%3D%20Get-MsolUser%20-UserPrincipalName%20%24MSOLUser%3CBR%20%2F%3E%24users%20%3D%20Get-MsolUser%20-All%20%7C%20Where-Object%20%7B%24_.isLicensed%20-eq%20%24true%20-and%20%24_.UserPrincipalName%20-notlike%20%22*%24Users*%22%7D%3CBR%20%2F%3E%24users.Count%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20appreciated!%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-353898%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-354701%22%20slang%3D%22en-US%22%3ERe%3A%20Get-MsolUser%20filtered%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-354701%22%20slang%3D%22en-US%22%3E%3CP%3EYes%2C%20you%20can%20do%20it%20as%20like%20in%20below%20script%3A%3C%2FP%3E%3CPRE%3E%24Results%20%7C%20ForEach-Object%20%7B%3CBR%20%2F%3E%24userObj%20%3D%20Get-MsolUser%20-UserPrincipalName%20%24_%3CBR%20%2F%3EWrite-Host%20%24userObj.DisplayName%3CBR%20%2F%3E%7D%3C%2FPRE%3E%3CP%3EBut%20what%20you%20are%20going%20to%20do%3F%20As%20we%20have%20already%20formed%20the%20%3CSTRONG%3E%24Results%3C%2FSTRONG%3E%20object%20from%20the%20output%20of%20%3CSTRONG%3EGet-MsolUser%3C%2FSTRONG%3E.%20I%20think%20you%20can%20extract%20required%20data%20from%20previous%20step%20itself.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20explain%20your%20need%20clearly%2C%20I%20will%20provide%20the%20updated%20script.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-354692%22%20slang%3D%22en-US%22%3ERe%3A%20Get-MsolUser%20filtered%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-354692%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20working%20perfectly!%20Thank%20you%20for%20your%20help.%3C%2FP%3E%3CP%3EOne%20final%20question%2C%20is%20it%20possible%20to%20pass%20the%20%24Results%20variable%20in%20to%20%22%24User%20%3D%20Get-MsolUser%20-UserPrincipalName%20%24Results%22%20%3F%20When%20i%20do%20this%20is%20I%20get%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EGet-MsolUser%20%3A%20Cannot%20convert%20'System.Object%5B%5D'%20to%20the%20type%20'System.String'%20required%20by%20parameter%20'UserPrincipalName'.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3ESpecified%20method%20is%20not%20supported.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EAt%20line%3A2%20char%3A41%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20%24User%20%3D%20Get-MsolUser%20-UserPrincipalName%20%24Results%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20~~~~~~~~%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20CategoryInfo%20%3A%20InvalidArgument%3A%20(%3A)%20%5BGet-MsolUser%5D%2C%20ParameterBindingException%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20FullyQualifiedErrorId%20%3A%20CannotConvertArgument%2CMicrosoft.Online.Administration.Automation.GetUser%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20depending%20where%20I%20insert%20the%20line%20I%20get%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EGet-MsolUser%20%3A%20Cannot%20convert%20'System.Object%5B%5D'%20to%20the%20type%20'System.String'%20required%20by%20parameter%20'UserPrincipalName'.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3ESpecified%20method%20is%20not%20supported.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EAt%20line%3A6%20char%3A41%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20%24User%20%3D%20Get-MsolUser%20-UserPrincipalName%20%24Results%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20~~~~~~~~%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20CategoryInfo%20%3A%20InvalidArgument%3A%20(%3A)%20%5BGet-MsolUser%5D%2C%20ParameterBindingException%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20FullyQualifiedErrorId%20%3A%20CannotConvertArgument%2CMicrosoft.Online.Administration.Automation.GetUser%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-354022%22%20slang%3D%22en-US%22%3ERe%3A%20Get-MsolUser%20filtered%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-354022%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20try%20this%20script%20(after%20replacing%20your%20UserName)%20%3A%3C%2FP%3E%3CPRE%3E%24Results%20%3D%20%40()%3B%3CBR%20%2F%3E%24Users%20%3D%20Get-Content%20C%3A%5CUsers%5CUserName%5CUserImport.txt%3CBR%20%2F%3E%23Get%20All%20Licensed%20Users%20%26amp%3B%20Filtered%3CBR%20%2F%3E%24licensedUsers%20%3D%20Get-MsolUser%20-All%20%7C%20Where-Object%20%7B%20%24_.isLicensed%20-eq%20%24true%20%7D%3CBR%20%2F%3E%24licensedUsers%20%7C%20ForEach-Object%20%7B%3CBR%20%2F%3EIf%20(%24Users%20-notcontains%20%24_.UserPrincipalName)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Results%20%2B%3D%20%22%24_.UserPrincipalName%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24Results.Length%3C%2FPRE%3E%3CP%3E%26nbsp%3BOr%20you%20can%20try%20this%20script%20%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24Results%20%3D%20%40()%3B%3CBR%20%2F%3E%24Users_Txt%20%3D%20Get-Content%20C%3A%5CUsers%5CUserName%5CUserImport.txt%20-Raw%3CBR%20%2F%3E%23Get%20All%20Licensed%20Users%20%26amp%3B%20Filtered%3CBR%20%2F%3E%24licensedUsers%20%3D%20Get-MsolUser%20-All%20%7C%20Where-Object%20%7B%20%24_.isLicensed%20-eq%20%24true%20%7D%3CBR%20%2F%3E%24licensedUsers%20%7C%20ForEach-Object%20%7B%3CBR%20%2F%3E%24upn%20%3D%20%24_.UserPrincipalName%3CBR%20%2F%3EIf%20(%24Users_Txt%20-notlike%20%22*%24upn*%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Results%20%2B%3D%20%24upn%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24Results.Length%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Deleted
Not applicable

Wonder if anyone can help...

> I have a txt file containing a list of user principal names. 

> I need to return all users if they have a licence and their UPN isn't listed in the TXT file. 

 

#User Import#
$Users = Get-Content C:\Users\NAME\UserImport.txt

#Get All Licensed Users & Filtered
foreach ($Users in $MSOLUsers){
$User = Get-MsolUser -UserPrincipalName $MSOLUser
$users = Get-MsolUser -All | Where-Object {$_.isLicensed -eq $true -and $_.UserPrincipalName -notlike "*$Users*"}
$users.Count
}

 

Any help appreciated! 

3 Replies
Highlighted
Solution

Hi,

 

You can try this script (after replacing your UserName) :

$Results = @();
$Users = Get-Content C:\Users\UserName\UserImport.txt
#Get All Licensed Users & Filtered
$licensedUsers = Get-MsolUser -All | Where-Object { $_.isLicensed -eq $true }
$licensedUsers | ForEach-Object {
If ($Users -notcontains $_.UserPrincipalName)
{
$Results += "$_.UserPrincipalName"
}
}
$Results.Length

 Or you can try this script :

 

$Results = @();
$Users_Txt = Get-Content C:\Users\UserName\UserImport.txt -Raw
#Get All Licensed Users & Filtered
$licensedUsers = Get-MsolUser -All | Where-Object { $_.isLicensed -eq $true }
$licensedUsers | ForEach-Object {
$upn = $_.UserPrincipalName
If ($Users_Txt -notlike "*$upn*")
{
$Results += $upn
}
}
$Results.Length

 

Highlighted

This is working perfectly! Thank you for your help.

One final question, is it possible to pass the $Results variable in to "$User = Get-MsolUser -UserPrincipalName $Results" ? When i do this is I get:

 

Get-MsolUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'UserPrincipalName'.
Specified method is not supported.
At line:2 char:41
+ $User = Get-MsolUser -UserPrincipalName $Results
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-MsolUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Online.Administration.Automation.GetUser

 

 

Or depending where I insert the line I get:

 

Get-MsolUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'UserPrincipalName'.
Specified method is not supported.
At line:6 char:41
+ $User = Get-MsolUser -UserPrincipalName $Results
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-MsolUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Online.Administration.Automation.GetUser

Highlighted

Yes, you can do it as like in below script:

$Results | ForEach-Object {
$userObj = Get-MsolUser -UserPrincipalName $_
Write-Host $userObj.DisplayName
}

But what you are going to do? As we have already formed the $Results object from the output of Get-MsolUser. I think you can extract required data from previous step itself.

 

If you explain your need clearly, I will provide the updated script.