Feb 20 2019 02:33 AM - edited Feb 20 2019 02:35 AM
Feb 20 2019 02:33 AM - edited Feb 20 2019 02:35 AM
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!
Feb 20 2019 07:03 AM - edited Feb 20 2019 07:04 AM
SolutionHi,
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
Feb 21 2019 05:03 AM
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
Feb 21 2019 05:29 AM
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.
Feb 20 2019 07:03 AM - edited Feb 20 2019 07:04 AM
SolutionHi,
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