The Select-Object cmdlet in PowerShell is used to select specific properties of objects or set of objects. This is a fundamental cmdlet often used for filtering and manipulating output in PowerShell, including when working with Microsoft Graph in PowerShell.
Microsoft Graph PowerShell uses Select-Object to extract specific information from the data objects returned by Graph API queries. This can be particularly useful when you are dealing with large sets of data and only need a few specific fields.
Here are a few examples to illustrate how you might use Select-Object in the context of Microsoft Graph PowerShell:
Here’s the basic syntax:
Select-Object [-Property] <ObjectProperty[]> [-ExcludeProperty <ObjectProperty[]>] [-Unique] [-Last <Int32>] [-First <Int32>] [-Skip <Int32>
]
Suppose you want to retrieve the display name and job title of a specific user in your organization. You could use a command like this:
This command retrieves the user information for "adelev@7xh7fj.onmicrosoft.com" and pipes it to Select-Object, which then selects and displays only the DisplayName and JobTitle properties.
If you want to list the first 5 users in your organization by their display name and ID, you could use:
This retrieves all users but pipes the results to Select-Object to only show the DisplayName and Id properties, limiting the output to the first 5 records.
Select-Object can also be used to create new, custom properties on the fly. For instance, if you need a full name property that isn't provided by default, you might do something like this:
This command creates a new custom property called FullName by combining the GivenName and Surname properties of each user object, and also selects the JobTitle property.
Alternatively, you can use Select-Object to exclude certain properties from the output. This can be useful when an object has many properties, but you want to omit just a few:
This command gets all properties for the user "adelev@7xh7fj.onmicrosoft.com" except for AboutMe, AgeGroup and Activities.
Let’s say you want to create a script that retrieves all Microsoft 365 Groups in your tenant and displays their names, IDs, and the number of members in each group. This script can help administrators quickly assess group usage and membership counts.
Here’s how you might write this script using Microsoft Graph PowerShell:
Ensure you do the following before running the script:
Let's create a script that retrieves a list of all applications registered in your Azure AD tenant along with their application IDs, display names, and the date they were created. This script can be especially useful for administrators to keep track of registered applications and their creation dates, aiding in audits or compliance checks.
Here’s how you might write this script using Microsoft Graph PowerShell:
Ensure you do the following before running the script:
Select-Object is one of the most versatile cmdlets in PowerShell, enabling you to manipulate and transform data in powerful ways. Here are some useful tips to maximize its potential:
What is the purpose of the Select-Object cmdlet in Graph PowerShell?
The Select-Object cmdlet is used to retrieve specific properties from objects in PowerShell. When working with Microsoft Graph PowerShell, it helps filter and format the output to display only the required information.
How is Select-Object different from Format-Table or Format-List?
Select-Object filters and retrieves properties from objects, making the data available for further processing. Format-Table or Format-List only change how data is displayed in the console but cannot be used for further processing.
Can I use Select-Object to rename properties?
No, Select-Object cannot rename properties directly. However, you can create a custom property like this:
Get-MgUser | Select-Object @{Name="FullName"; Expression={$_.GivenName + " " + $_.Surname}}, Mail
How can I limit the number of results using Select-Object?
Use the -First parameter to limit the number of results:
Get-MgUser -All | Select-Object DisplayName, Id -First 5
This will retrieve only the first 5 users.
© m365corner.com. All Rights Reserved. Design by HTML Codex