SOLVED

Get-MsolUser filtered variable

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
best response
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

 

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

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.

1 best response

Accepted Solutions
best response
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

 

View solution in original post