Connecting to Microsoft Graph using PowerShell with a Client ID (also known as Application ID or AppID) and a Client Secret is a common requirement for automating tasks and running scripts in non-interactive, unattended scenarios. This method leverages the OAuth 2.0 client credentials grant flow, allowing an application to authenticate itself directly without user intervention. This guide will walk you through the entire process, from prerequisites to execution.
SecureString and then packaging it, along with the Client ID, into a PSCredential object. This object is then passed to the Connect-MgGraph cmdlet.Before you can connect to Microsoft Graph using a Client ID and Client Secret, ensure you have the following in place:
You need to register an application in the Microsoft Entra admin center (formerly Azure AD portal). During or after registration, you will obtain/configure the following:
Interface showing aspects of Azure AD App Registration, a prerequisite for obtaining credentials.
The Microsoft Graph PowerShell SDK is required to use the Connect-MgGraph cmdlet. If you haven't already, install it by running the following command in PowerShell (preferably as an administrator or using the `-Scope CurrentUser` for non-admin installs):
Install-Module Microsoft.Graph -Scope CurrentUser -Force
It's also good practice to ensure your modules are up to date:
Update-Module Microsoft.Graph
The SDK includes various sub-modules, including `Microsoft.Graph.Authentication`, which contains `Connect-MgGraph`.
Once the prerequisites are met, you can use the following PowerShell script structure to connect. This method involves creating a `PSCredential` object that securely holds your application's Client ID and Client Secret.
Store your Azure AD app registration details in variables. Replace the placeholder values with your actual credentials.
# Azure AD App Registration Details
$clientId = "YOUR_APPLICATION_CLIENT_ID" # Also known as AppID
$tenantId = "YOUR_DIRECTORY_TENANT_ID"
$clientSecretValue = "YOUR_CLIENT_SECRET_VALUE"
The Client Secret needs to be converted into a `SecureString` object for use in the `PSCredential`.
# Convert the Client Secret to a SecureString
$secureClientSecret = ConvertTo-SecureString -String $clientSecretValue -AsPlainText -Force
Create a `PSCredential` object. The `UserName` parameter will be your application's Client ID, and the `Password` parameter will be the `SecureString` version of your Client Secret.
# Create the PSCredential object
$credential = New-Object System.Management.Automation.PSCredential($clientId, $secureClientSecret)
Use the Connect-MgGraph cmdlet with the -TenantId and -ClientSecretCredential parameters. For app-only authentication, it's common to use the -Scopes "https://graph.microsoft.com/.default" parameter. This scope tells Microsoft Graph to grant the application the permissions that were pre-configured and consented to in its Azure AD registration, rather than requesting specific scopes dynamically.
# Connect to Microsoft Graph using the Client Secret Credential
Connect-MgGraph -TenantId $tenantId -ClientSecretCredential $credential -Scopes "https://graph.microsoft.com/.default"
After running the connect command, you can verify the connection by attempting a simple Microsoft Graph query, such as retrieving a list of users (assuming your app has the `User.Read.All` permission):
# Example: Get the top 5 users to verify connection and permissions
try {
Get-MgUser -Top 5 | Select-Object Id, DisplayName, UserPrincipalName
Write-Host "Successfully connected and retrieved data from Microsoft Graph."
} catch {
Write-Error "Failed to retrieve data. Error: $($_.Exception.Message)"
Write-Warning "Ensure your application has the necessary API permissions (e.g., User.Read.All) and admin consent in Azure AD."
}
# Script to Connect to Microsoft Graph using Client ID and Client Secret
# Suppress breaking change messages for a cleaner output (optional)
# Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\PowerShell\Microsoft Graph" -Name ShowMSGraphDeprecation -Value $false -ErrorAction SilentlyContinue
# --- Configuration: Replace with your actual values ---
$clientId = "YOUR_APPLICATION_CLIENT_ID"
$tenantId = "YOUR_DIRECTORY_TENANT_ID"
$clientSecretValue = "YOUR_CLIENT_SECRET_VALUE" # Ensure this is handled securely in production
# ----------------------------------------------------
# Convert Client Secret to SecureString
$secureClientSecret = ConvertTo-SecureString -String $clientSecretValue -AsPlainText -Force
# Create PSCredential object
# The Client ID is used as the 'username' part of the credential
$credential = New-Object System.Management.Automation.PSCredential($clientId, $secureClientSecret)
try {
Write-Host "Attempting to connect to Microsoft Graph..."
# Connect using Client Secret Credential for app-only authentication
Connect-MgGraph -TenantId $tenantId -ClientSecretCredential $credential -Scopes "https://graph.microsoft.com/.default"
# Confirm connection status
$context = Get-MgContext
if ($context.Account) {
Write-Host "Successfully connected to Microsoft Graph!"
Write-Host "Tenant ID: $($context.TenantId)"
Write-Host "App ID: $($context.ClientId)"
Write-Host "AuthType: $($context.AuthType)"
Write-Host "Scopes: $($context.Scopes -join ', ')"
# Example command: List top 2 users (requires User.Read.All)
Write-Host "`nFetching top 2 users (requires User.Read.All permission):"
Get-MgUser -Top 2 | Select-Object DisplayName, UserPrincipalName, Id
} else {
Write-Warning "Connection appeared to succeed, but context is not fully populated. Check authentication."
}
} catch {
Write-Error "An error occurred during connection or data retrieval: $($_.Exception.Message)"
Write-Warning "Troubleshooting tips:"
Write-Warning "- Verify Client ID, Tenant ID, and Client Secret are correct."
Write-Warning "- Ensure the Client Secret has not expired."
Write-Warning "- Confirm the application has the required API permissions (e.g., User.Read.All for Get-MgUser) in Azure AD."
Write-Warning "- Ensure admin consent has been granted for those permissions."
Write-Warning "- Check if the Microsoft.Graph module is installed and up-to-date."
} finally {
# Optional: Disconnect when done if running multiple connection types in a script
# Write-Host "Disconnecting from Microsoft Graph..."
# Disconnect-MgGraph
}
While connecting with a Client ID and Client Secret is effective for many automation scenarios, Microsoft Graph supports various authentication methods. The choice depends on factors like security requirements, the environment where the script runs, and ease of management. The radar chart below provides a conceptual comparison of common methods.
This chart visualizes characteristics of different authentication approaches:
Each axis on the radar chart represents a desirable attribute, with higher values (further from the center) indicating better performance in that aspect. For instance, 'Security' is highest for Managed Identity and Certificate Authentication. 'Ease of Setup for Automation' might be perceived as higher for Client Secret initially, though it comes with management overhead.
The following mindmap visualizes the key stages involved in setting up and using Client ID and Client Secret authentication with Connect-MgGraph in PowerShell. It highlights the journey from initial Azure AD configuration to executing Graph API calls.
This mindmap provides a structured overview, showing how each component contributes to the overall goal of establishing an authenticated session to Microsoft Graph for automated tasks.
The following video provides a walkthrough of app registration in Microsoft Entra ID (Azure AD) and connecting to Microsoft Graph from PowerShell, touching upon concepts like client secrets and `PSCredential` objects. While the video may cover broader topics, segments are highly relevant to understanding the setup required for client credential flow.
Video: "Entra App registration - Step-by step part 1 (MS Graph from PowerShell)". This video discusses app registration and connecting with secrets, relevant to the `Connect-MgGraph` process. Key timestamps might include discussions around the authentication module and connecting with a secret/PSCredential (around the 10:00-10:45 mark as per its description).
Watching this can provide a practical demonstration of the Azure portal steps and how they translate into the PowerShell script elements discussed. Understanding the app registration process is fundamental, as misconfigurations here are common sources of connection errors.
To successfully connect, you need specific pieces of information, primarily sourced from your application's registration in Microsoft Entra ID. The table below summarizes these essential parameters:
| Parameter Name | Description | Source in Azure Portal (Microsoft Entra ID) |
|---|---|---|
| Tenant ID (Directory ID) | The unique identifier of your Microsoft Entra ID tenant. | Navigate to Microsoft Entra ID > Overview page. Also available in App registrations > [Your App] > Overview. |
| Client ID (Application ID / AppID) | The unique identifier for your registered application. | Navigate to App registrations > [Your App] > Overview page. |
| Client Secret (Application Password) | A secret string generated for your application, used for authentication. | Navigate to App registrations > [Your App] > Certificates & secrets > Client secrets section. Remember to copy the secret's value immediately after creation. |
Ensuring these values are correct and that the Client Secret has not expired is critical for a successful connection.
Update-Module Microsoft.Graph) to benefit from the latest features, cmdlets, and security fixes.This is one of the most common errors. It means the Azure AD application does not have the required API permissions for the Microsoft Graph operation you are attempting, or admin consent has not been granted for those permissions. Solution: Verify the application permissions (must be 'Application' type for this flow) in Azure AD for your app registration and ensure an administrator has granted consent.
This can happen if the Client ID, Tenant ID, or Client Secret value is incorrect, or if the Client Secret has expired. Solution: Double-check all credential values. If the secret has expired, generate a new one in the Azure portal and update your script.
Connect-MgGraph Cmdlet Not FoundThis indicates that the Microsoft Graph PowerShell SDK, specifically the `Microsoft.Graph.Authentication` module, is not installed or not imported correctly into the session.
Solution: Ensure the module is installed (Install-Module Microsoft.Graph) and try importing it explicitly if needed (Import-Module Microsoft.Graph.Authentication).
PSCredential ObjectEnsure the Client ID is passed as the username and the SecureString version of the Client Secret is passed as the password when creating the PSCredential object.
Solution: Review the script lines where ConvertTo-SecureString and New-Object System.Management.Automation.PSCredential are used.
-Scopes "https://graph.microsoft.com/.default"?
To deepen your understanding of Microsoft Graph and PowerShell automation, consider exploring these related topics: