Forum Discussion
Get-MsolUser filtered variable
- Feb 20, 2019
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.LengthOr 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
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
- DeletedFeb 21, 2019
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.GetUserOr 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- Kevin_MorganFeb 21, 2019Iron Contributor
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.