Using Select-Object In Graph PowerShell

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:


Basic Syntax

Here’s the basic syntax:
Select-Object [-Property] <ObjectProperty[]> [-ExcludeProperty <ObjectProperty[]>] [-Unique] [-Last <Int32>] [-First <Int32>] [-Skip <Int32> ]

  • -Property: Specifies the properties to select from the input objects. You can list properties individually or use wildcards. If you do not specify this parameter, all properties are selected.
  • -ExcludeProperty: Specifies properties that should be excluded from the selection.
  • -Unique: Instructs Select-Object to return only unique items from the selection based on the properties specified.
  • -First: Specifies the number of objects to select from the beginning of an array or collection.
  • -Last: Specifies the number of objects to select from the end of an array or collection.
  • -Skip: Skips the specified number of objects and then selects the remaining objects.


Selecting Specific Properties of a User

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.


Selecting Top N Records

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.



Using Custom Properties

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.


Excluding Properties

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.



Get group information and select/display specific properties

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:

You can download the script here: use-select-object-to-display-specific-group-properties.ps1

How the Script Works?

  • Retrieve and Process Group Data: The Get-MgGroup -All cmdlet retrieves all groups. Each group is then processed in a ForEach-Object loop to count the members by calling Get-MgGroupMember for each group's ID.
  • Creating Custom Objects: Inside the loop, a custom object is created for each group that includes the group’s display name, ID, and the number of members. This is achieved using [PSCustomObject].
  • Display the Data: The resulting list of custom objects is then piped to Select-Object to neatly display the DisplayName, Id, and MemberCount for each group.

Notes

Ensure you do the following before running the script:

  • Connect to Microsoft Graph PowerShell: Read Connect to Graph PowerShell for more info.
  • Get the Necessary Graph Permissions You will need User.Read.All and Group.Read.All Graph API permissions.

Get app information and select/display specific properties

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:

You can download the script here: use-select-object-to-display-available-apps.ps1

How the Script Works?

  • Retrieve and Process Application Data: Get-MgApplication -All retrieves all applications registered in the tenant. The -All parameter ensures that pagination is handled automatically if there are more results than can be returned in a single response.
  • Custom Property Selection The Select-Object cmdlet is used to filter and format the output. It selects the DisplayName, AppId, and converts the CreatedDateTime from UTC to local time using a calculated property. The expression {_.CreatedDateTime.ToLocalTime()} modifies the CreatedDateTime property to be more readable.
  • Display the Data: The script outputs the selected and formatted application data.

Notes

Ensure you do the following before running the script:

  • Connect to Microsoft Graph PowerShell: Read Connect to Graph PowerShell for more info.
  • Get the Necessary Graph Permissions You will need Application.Read.All Graph API permission.

Tips for Using Select-Object

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:

  • Use Property Aliases: Often, PowerShell objects have property aliases that can simplify your commands. You can use Get-Member to explore properties and their aliases, which can make your Select-Object commands more concise.
  • Creating Custom Properties: One of the most powerful features of Select-Object is the ability to create custom properties. This allows you to add new properties to the output objects that are derived from existing properties and suit your scripting requirements.
  • Filtering with Wildcards: When you're dealing with objects that have many properties, you can use wildcards to select properties that match a certain pattern. This is particularly useful when you only need properties that include a specific keyword.
  • Optimize Performance by Limiting Output Early: In a pipeline, place Select-Object early if you only need a few properties from a potentially large set of data. This reduces the amount of data processed in subsequent commands, improving performance.
  • Exporting Selected Data: After using Select-Object to isolate and structure the data you need, consider exporting it to a CSV or XML file for further analysis or reporting.

Related Articles:

Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex