Using Graph PowerShell Functions for Managing Microsoft 365 Tenant

Graph PowerShell functions are an essential tool for managing and automating tasks in a Microsoft 365 (M365) environment. They allow administrators to execute complex operations with reusable and modular scripts enhancing productivity and ensuring consistency across their tasks.

This article covers creating different types of PowerShell functions, including simple functions, functions with parameters, functions with default parameters, functions with multiple parameters, and functions returning values. We'll provide usage examples in the context of querying an M365 tenant, ensuring that these examples are straightforward and easy to understand.


Basics of PowerShell Functions

A PowerShell function is a block of code designed to perform a specific task. Functions make scripts more modular and reusable which is especially useful in managing M365 environments. Here's how you can create different types of functions in PowerShell.

Simple Function

A simple function does not take any parameters. It performs a predefined task whenever it is called.

Function Get-M365TenantInfo {
    Write-Output "Fetching Microsoft 365 Tenant Information..."
    # Example command to get tenant information
    Get-MgOrganization
}

Function with Parameters

A function with parameters allows you to pass data into the function making it more flexible and versatile.

Function Get-M365UserInfo {
    param (
        [string]$UserPrincipalName
    )
    Write-Output "Fetching information for user: $UserPrincipalName"
    # Example command to get user information
    Get-MgUser -UserId $UserPrincipalName
}

Function with Default Parameter Values

A function with default parameter values provides default values for parameters which can be overridden when calling the function.

Function Get-M365GroupInfo {
    param (
        [string]$GroupId = "default-group-id"
    )
    Write-Output "Fetching information for group: $GroupId"
    # Example command to get group information
    Get-MgGroup -GroupId $GroupId
}

Function with Multiple Parameters

A function with multiple parameters allows for more complex operations enabling you to pass various pieces of data into the function.

Function Get-M365UserLicenseInfo {
    param (
        [string]$UserPrincipalName
        [switch]$IncludeDetails
    )
    Write-Output "Fetching license information for user: $UserPrincipalName"
    if ($IncludeDetails) {
        # Example command to get detailed user license information
        Get-MgUserLicenseDetail -UserId $UserPrincipalName
    } else {
        # Example command to get basic user license information
        Get-MgUser -UserId $UserPrincipalName -Property assignedLicenses
    }
}

Function Returning Values

A function can also return values which can be used in further processing or logic.

Function Get-M365UserEmail {
    param (
        [string]$UserPrincipalName
    )
    $user = Get-MgUser -UserId $UserPrincipalName
    return $user.Mail
}

Usage Examples in M365 Context

Example 1: Fetching Tenant Information

Using the simple function Get-M365TenantInfo to fetch basic tenant information:

Get-M365TenantInfo

Example 2: Fetching User Information

Using the parameterized function Get-M365UserInfo to fetch user information:

Get-M365UserInfo -UserPrincipalName "john.doe@contoso.com"

Example 3: Fetching Group Information with Default Parameter

Using the function Get-M365GroupInfo with the default parameter:

Get-M365GroupInfo

Example 4: Fetching User License Information

Using the function Get-M365UserLicenseInfo to fetch user license information with multiple parameters:

Get-M365UserLicenseInfo -UserPrincipalName "john.doe@contoso.com" -IncludeDetails

Example 5: Getting User Email

Using the function Get-M365UserEmail to get a user's email:


Conclusion

PowerShell functions are a powerful tool for automating and managing tasks in an M365 tenant. By creating simple functions parameterized functions functions with default parameters functions with multiple parameters and functions returning values administrators can streamline their workflows and make their scripts more modular and reusable.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
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
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

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