The Where-Object cmdlet in PowerShell is used for filtering data based on certain conditions. When you are working with Microsoft Graph PowerShell, you can use Where-Object to filter objects returned from Graph API queries based on their properties. This is particularly useful for narrowing down results to match specific criteria, especially when dealing with large sets of data from Microsoft Graph.
Here’s the basic syntax: Get-Command | Where-Object {$_.CommandType -eq 'Cmdlet'}
Get-Command retrieves all commands, and Where-Object filters these commands to include only those whose CommandType property is 'Cmdlet'.
Suppose you want to find all users in your organization whose email addresses are in a specific domain (e.g., "7xh7fj.onmicrosoft.com"). You can use the Get-MgUser cmdlet to fetch the users and then filter these using Where-Object as follows: Get-MgUser -All | Where-Object ($_.Mail -like "@7xh7fj.onmicrosoft.com"
This command fetches all users (-All to ensure it fetches more than the default page size limit) and filters them to include only those whose Mail property ends with "@7xh7fj.onmicrosoft.com".
If you want to retrieve users who have a specific job title, such as "Software Developer", you can do the following: Get-MgUser -Filter "jobTitle eq 'Software Developer'"
Note: When possible, it's more efficient to use the -Filter parameter that some Microsoft Graph PowerShell cmdlets support, as it processes the filter on the server side rather than retrieving all objects and filtering them client-side with Where-Object.
To find active Microsoft Teams in your tenant, execute the following command: Get-MgGroup -All | Where-Object {$_.GroupTypes -contains "Unified" -and $_.MailEnabled-eq $true}
This example fetches all groups and filters to find only those that are Unified Groups (which include Teams) and are mail-enabled.
You should run the following command: Get-MgUser -All -Select "DisplayName, AssignedLicenses" | Where-Object {$_.AssignedLicenses.SkuId -contains "c42b9cae-ea4f-4ab7-9717-81576235ccac"}
. In this, replace "c42b9cae-ea4f-4ab7-9717-81576235ccac" with the actual SKU ID of the license you're looking for. This command fetches all users and filters them to find only those with an assigned license that matches the specified SKU ID.
Note: Run Get-MgSubscribedSku -All to get all the license IDs available within your tenant.
Finding inactive users is a common administrative task, typically focused on users who haven't logged in for a certain period, such as the past 90 days. Here’s how you might approach this using Microsoft Graph PowerShell:
$thresholdDate = (Get-Date).AddDays(-90)
Get-MgUser -All | Where-Object {-not $_.SignInActivity.LastSignInDateTime -or $_.SignInActivity.LastSignInDateTime -lt $thresholdDate}
This script first calculates a date that is 90 days before the current date. It then retrieves all users and filters out those whose last sign-in date is either not set (-not $_.SignInActivity.LastSignInDateTime) or older than 90 days ($_.SignInActivity.LastSignInDateTime -lt $thresholdDate). This helps in identifying users who are potentially inactive.
What is the Where-Object cmdlet used for in Graph PowerShell?
The Where-Object cmdlet is used to filter objects based on specified conditions. It helps refine results when working with Microsoft Graph PowerShell by selecting only the items that meet certain criteria.
How is Where-Object different from the -Filter parameter?
The -Filter parameter is processed server-side, making it more efficient as only relevant data is retrieved. Where-Object, on the other hand, filters results after fetching all the data, which can be slower but offers more flexibility.
Can I use multiple conditions with Where-Object?
Yes, you can use multiple conditions by combining them with logical operators like -and or -or.
Get-MgUser | Where-Object { $_.Department -eq "IT" -and $_.JobTitle -eq "Admin" }
Can Where-Object be used with Graph PowerShell’s Get-MgUser cmdlet?
Yes, it can be used to filter users based on attributes like department, job title, or last sign-in activity.
Get-MgUser -All | Where-Object { $_.UserPrincipalName -like "*@yourdomain.com" }
What’s the best practice: Where-Object or -Filter?
If the -Filter parameter is available, use it for better performance, as it reduces the amount of data retrieved. If -Filter isn’t supported or needs more complex logic, then use Where-Object
Using Where-Object effectively with Microsoft Graph can help you manage and automate tasks related to Office 365 and other Microsoft services, making it a powerful tool in your PowerShell scripting arsenal.
Read Microsoft PowerShell Where-Object documentation for more info.
© m365corner.com. All Rights Reserved. Design by HTML Codex