Using -Contains Operator In Graph PowerShell

The -contains operator in PowerShell is used to check if a collection contains a specified item. When working with Microsoft Graph PowerShell, which is used to manage and interact with Microsoft 365 services, the -contains operator can be useful to filter and check for the presence of specific items in collections returned by various cmdlets.

This article lists some use cases involving -contains parameter, along with its basic syntax.


Basic Syntax

<collection> -contains <item>

  • <collection>: the collection of items you want to search through. It can be an array, a list, or any collection of objects.
  • -contains: This is the operator that performs the check. It returns True if the item is found in the collection and False otherwise.
  • <item>: This is the specific item you are looking for within the collection.

Checking Item in Collection

  • $fruits is an array (a type of collection). -contains operator is used to check whether 'apple' and 'grape' values are present in it.
  • -contains operator returns True if the value is present, else it returns False (meaning value is not present)

Using -contains in If-Else Statement

-contains can be used in conditional statements to perform actions based on the presence or absence of an item in a collection.

Since $fruits contains banana, Banana is in the list message gets outputted.


Configuration Management

Check if specific configurations or settings are applied by verifying their presence in configuration files or arrays.



User Input Validation

Validate user inputs by checking if they exist in a predefined set of acceptable values.



Security and Access Control

Verify if a user has specific permissions or roles by checking their presence in access control lists or role assignments.



Automation and Monitoring

-contains operator can be used in automation scripts to check the status of services or components and take actions based on their presence in status lists.



Checking if a User is in a Group

Let's say you want to check if a specific user is a member of a particular group. You would first retrieve the group's members and then use the -contains operator to see if the user is in the list. powershell


How the Script Works?

  1. Get Group ID and User ID: $groupId represents the ID of the group you want to check, and $userId represents the ID of the user you want to verify as a member of that group.
  2. Get Group Members: Next get all the members in the group using Get-MgGroupMember cmdlet and passing the $groupId as the parameter.
  3. Check if the User is in the Group: Use -contains operator to check if the $userId is present in the list of group member IDs ($groupMembers.Id)
  4. Conditional Check and Output: This block checks the value of $userInGroup. If it's True, it outputs "The user is in the group." Otherwise, it outputs "The user is not in the group."

Check if a User Has Administrator Role


How the Script Works?

  1. Install the Microsoft Graph PowerShell SDK: The SDK provides cmdlets for interacting with Microsoft Graph, which is required for managing and querying Azure AD and other Microsoft 365 services.
  2. Connect to Microsoft Graph: This command establishes a connection to Microsoft Graph and requests the permission scope RoleManagement.Read.Directory.
  3. Get all directory roles: This command retrieves all directory roles available in the Azure AD tenant.
  4. Find the Global Administrator role: The Where-Object cmdlet filters the list of roles to find the one with the display name "Global Administrator". It then retrieves the ID of this role and assigns it to the variable $roleId.
  5. Define user ID: Then pass the user id to the $userId variable.
  6. Get all members of the Global Administrator role: This command retrieves all members assigned to the Global Administrator role using the role ID.
  7. Check if the user is in the list of role members: This line checks if the specified user ID is in the list of IDs of the Global Administrator role members.
  8. Output the result: This block of code evaluates the $userInRole variable. If it is True, it outputs "The role is assigned to the user." Otherwise, it outputs "The role is not assigned to the user."

Using -Contains Operator With Where-Object

Assume we have a list of user objects, each with properties like Name and Department. We want to filter this list to find users who are in specific departments (e.g., "IT" and "HR"). This can be accomplished by combining where-object with -contains operator as follows:


How the Script Works?

  1. Sample List of User Objects: We create a list of user objects with Name and Department properties using [PSCustomObject].
  2. Target Departments: We define an array $targetDepartments containing the departments we are interested in ("IT" and "HR").
  3. Filter Users: We use the Where-Object cmdlet to filter the list of users. Inside the script block, we use the -contains operator to check if the Department property of each user is in the $targetDepartments array.
  4. Display Results: The filtered users are displayed using Format-Table for a nicely formatted output.

Benefits of Using -Contains Operator


  • Simplicity and Readability:: The -contains operator provides a straightforward and readable way to check if an item exists in a collection.
  • Efficiency: It efficiently checks for the presence of an item in a collection, making it faster and more suitable for quick membership tests. This is particularly useful when working with large collections where performance matters.
  • Conditional Logic:: The -contains operator can be used in conditional statements to control the flow of scripts based on whether an item is in a collection.

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
Using Select Object In Graph PowerShell

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