How can I filter AzureAD users by display name using PowerShell?

0
5
Asked By TechWizard2023 On

I'm running this PowerShell command to get some AzureAD users: `Get-AzureADUserOwnedObject -ObjectId *object-id* | Select-Object ObjectID, ObjectType, DisplayName`, but I need to filter the results based on the display name. Unfortunately, the command doesn't support filtering natively. I think it should be simple for someone well-versed in PowerShell to help me out!

3 Answers

Answered By HelperBot92 On

You can totally use `Where-Object` for filtering. Just avoid using 'starts with'; instead, go with `-like` and wildcards. Here's an adjusted command:

```powershell
Get-AzureADUserOwnedObject -ObjectId *object-id* | Where-Object { $_.DisplayName -like "*thingyouarefiltering*" } | Select-Object ObjectID, ObjectType, DisplayName
```

This will give you the filtered results you need! Just a heads up, you can even place `Where-Object` at the end as long as you include the properties you're filtering on in your `Select-Object`.

CuriousCoder76 -

Got it! Now, any idea how I could set the group owner using similar criteria?

FilterEnthusiast88 -

Yes! With the wildcards, that makes perfect sense. Thanks for that tip!

Answered By PowershellPro47 On

Totally! In PowerShell, you can either prefilter or postfilter your results. If you can't prefilter, `Where-Object` is your go-to.

Here's the basic usage:

```powershell
$list | Where-Object { $_.DisplayName -like '*infix*' }
```

The asterisks `*` are wildcards you can use where you don't care what the text is. Also, remember you can use it as an expression with curly braces if more complex filtering is needed. Familiarizing yourself with `Where-Object` is definitely worth it if you plan to work with PowerShell frequently!

Answered By ScriptMaster3000 On

Alternatively, you can do this:

```powershell
Get-AzureADUserOwnedObject -ObjectId object-id | Where-Object { $_.DisplayName -startsWith "xyz" } | Select-Object ObjectID, ObjectType, DisplayName
```

Just make sure to replace `-startsWith` with the filtering method that suits your needs. Minor point though—are you still using the AzureAD Modules? They're deprecated now, and switching to Microsoft.Graph would be a smart move since it supports most old commands still.

TechWizard2023 -

Thanks for the quick response! I’m aware MS is moving to Graph.

I tried your command, but I got an 'unexpected token' error. If I wrap it in single quotes, it doesn't error, but it also doesn't find any objects.

ScriptMaster3000 -

Hmm, make sure you're using it without quotes around the script block. Just keep it clean like this:

```powershell
Where-Object { $_.DisplayName -startsWith "xyz" }
```

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.