1

I am trying to automate my O365 licensing and have had some success, but I am not sure how I can accomplish the following...

I have users who I do not wish to license and they all share the value "svc" in their DisplayName.

In trying to test how this would work I'm dumping the values returned out to a CSV file, but no matter what I do I cannot exclude users with those values in their names. I can do a "contains" and it finds just them...but when I try a "notcontains" I just get all of the users including those with "svc" in their name.

How can I exclude users that have "svc" in their names? Here is my code...

$SyncUser = get-msoluser -all | Where-Object { $_.isLicensed -ne "TRUE" }

Foreach ($a in $SyncUser){

    if ($a.DisplayName.NotContains("svc")) {

        add-content -value ($a.Userprincipalname) -path $Logfile
    }
}

2 Answers 2

1

You could exclude these users like this:

$users = get-msoluser -all | Where-Object { (-not $_.isLicensed) -and  ($_.DisplayName -notmatch "svc")}

References

about_Comparison_Operators

3

Try this

$SyncUser = get-msoluser -all | Where-Object { $_.isLicensed -ne "TRUE" }
Foreach ($a in $SyncUser){
    if ($a.DisplayName.Contains("svc") -eq $False) {
        add-content -value ($a.Userprincipalname) -path $Logfile
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.