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.
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.
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
}
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
}
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
}
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
}
}
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
}
Using the simple function Get-M365TenantInfo to fetch basic tenant information:
Get-M365TenantInfo
Using the parameterized function Get-M365UserInfo to fetch user information:
Get-M365UserInfo -UserPrincipalName "john.doe@contoso.com"
Using the function Get-M365GroupInfo with the default parameter:
Get-M365GroupInfo
Using the function Get-M365UserLicenseInfo to fetch user license information with multiple parameters:
Get-M365UserLicenseInfo -UserPrincipalName "john.doe@contoso.com" -IncludeDetails
Using the function Get-M365UserEmail to get a user's email:
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.
© m365corner.com. All Rights Reserved. Design by HTML Codex