Backup Intune Configuration

This script connects to Microsoft Graph and exports the core Intune configuration surfaces to a timestamped backup folder: device configuration profiles, settings catalog policies (including their full setting bodies), compliance policies, administrative template (ADMX) policies with their definition values, Windows PowerShell scripts, and macOS shell scripts. Each object is written as one JSON file including its assignments, and a manifest summarizes the backup. The output is designed to be stored in source control for change tracking and used as input for the companion restore-intune-configuration script.

Configuration
2 views2 downloadsVersion 1.0By Ugur Koc
View on GitHub

New to runbook deployment? Follow the step-by-step guide from the Deploy to Azure button to the first scheduled run, including granting Graph permissions to the managed identity.

// QUALITY CHECKS

Validation status

Quality checks

All checks pass
  • ParsePass
  • LintPass
  • MetadataPass
  • Runbook-readyPass
  • Module depsPass

Tests run automatically on every change. What does each check mean?

// REQUIRED PERMISSIONS

Microsoft Graph scopes

DeviceManagementConfiguration.Read.All

Allows the app to read properties of Microsoft Intune-managed device configuration and device compliance policies and their assignment to groups, without a signed-in user.

Running this as an Azure Automation runbook? These scopes must be granted to the account's managed identity, which has no portal UI. The deployment walkthrough shows the exact Cloud Shell commands.

// CHANGELOG

Version history

  1. Entry · 01

    1.0 - Initial release

// CODE

Source

backup-intune-configuration.ps1
<#
.TITLE
    Backup Intune Configuration

.SYNOPSIS
    Exports Intune configuration profiles, compliance policies, ADMX policies, and platform scripts to JSON files for backup and versioning.

.DESCRIPTION
    This script connects to Microsoft Graph and exports the core Intune configuration
    surfaces to a timestamped backup folder: device configuration profiles, settings
    catalog policies (including their full setting bodies), compliance policies,
    administrative template (ADMX) policies with their definition values, Windows
    PowerShell scripts, and macOS shell scripts. Each object is written as one JSON
    file including its assignments, and a manifest summarizes the backup. The output
    is designed to be stored in source control for change tracking and used as input
    for the companion restore-intune-configuration script.

.TAGS
    Configuration

.MINROLE
    Intune Administrator

.PERMISSIONS
    DeviceManagementConfiguration.Read.All

.AUTHOR
    Ugur Koc

.VERSION
    1.0

.CHANGELOG
    1.0 - Initial release

.LASTUPDATE
    2026-07-20

.EXAMPLE
    .\backup-intune-configuration.ps1
    Exports all supported configuration areas to a timestamped folder in the current directory

.EXAMPLE
    .\backup-intune-configuration.ps1 -OutputPath "C:\IntuneBackups" -Areas DeviceConfigurations,CompliancePolicies
    Exports only classic configuration profiles and compliance policies to C:\IntuneBackups

.EXAMPLE
    .\backup-intune-configuration.ps1 -SkipScriptContent
    Exports all areas but skips downloading the base64 script bodies of platform scripts

.NOTES
    - Requires Microsoft.Graph.Authentication module
    - Uses beta Graph endpoints because the full Intune configuration surface is not exposed on v1.0
    - Settings catalog setting bodies and ADMX definition values require one extra request per policy
    - Graph never returns secret values (encrypted OMA-URI settings, passwords, certificates) in exports; those settings appear with secret references only and must be re-entered manually after a restore
    - Local interactive sign-in uses the MgGraphCommunity module to avoid the Graph SDK's mandatory WAM broker on Windows
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory = $false, HelpMessage = "Folder in which the timestamped backup folder is created")]
    [ValidateNotNullOrEmpty()]
    [string]$OutputPath = ".",

    [Parameter(Mandatory = $false, HelpMessage = "Configuration areas to export")]
    [ValidateSet("DeviceConfigurations", "SettingsCatalog", "CompliancePolicies", "AdmxPolicies", "PlatformScripts")]
    [string[]]$Areas = @("DeviceConfigurations", "SettingsCatalog", "CompliancePolicies", "AdmxPolicies", "PlatformScripts"),

    [Parameter(Mandatory = $false, HelpMessage = "Skip downloading base64 script bodies of platform scripts")]
    [switch]$SkipScriptContent,

    [Parameter(Mandatory = $false, HelpMessage = "Force module installation without prompting")]
    [switch]$ForceModuleInstall
)

# ============================================================================
# ENVIRONMENT DETECTION AND SETUP
# ============================================================================

function Initialize-RequiredModule {
    param(
        [string[]]$ModuleNames,
        [bool]$IsAutomationEnvironment,
        [bool]$ForceInstall = $false
    )

    foreach ($ModuleName in $ModuleNames) {
        Write-Verbose "Checking module: $ModuleName"

        $module = Get-Module -ListAvailable -Name $ModuleName | Select-Object -First 1

        if (-not $module) {
            if ($IsAutomationEnvironment) {
                throw "Module '$ModuleName' is not available in Azure Automation"
            }
            else {
                Write-Information "Module '$ModuleName' not found. Installing..." -InformationAction Continue

                if (-not $ForceInstall) {
                    $response = Read-Host "Install module '$ModuleName'? (Y/N)"
                    if ($response -notmatch '^[Yy]') {
                        throw "Module '$ModuleName' is required but installation was declined."
                    }
                }

                try {
                    $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
                    $scope = if ($isAdmin) { "AllUsers" } else { "CurrentUser" }

                    Install-Module -Name $ModuleName -Scope $scope -Force -AllowClobber -Repository PSGallery
                    Write-Information "✓ Successfully installed '$ModuleName'" -InformationAction Continue
                }
                catch {
                    throw "Failed to install module '$ModuleName': $($_.Exception.Message)"
                }
            }
        }

        Import-Module -Name $ModuleName -Force -ErrorAction Stop
    }
}

# Detect execution environment
$IsAzureAutomation = $null -ne $PSPrivateMetadata.JobId.Guid

# Initialize required modules
$RequiredModules = @("Microsoft.Graph.Authentication")

# MgGraphCommunity gives WAM-free interactive sign-in for local runs
if (-not $IsAzureAutomation) {
    $RequiredModules += "MgGraphCommunity"
}

try {
    Initialize-RequiredModule -ModuleNames $RequiredModules -IsAutomationEnvironment $IsAzureAutomation -ForceInstall $ForceModuleInstall
    Write-Verbose "✓ All required modules are available"
}
catch {
    Write-Error "Module initialization failed: $_"
    exit 1
}

# ============================================================================
# AUTHENTICATION
# ============================================================================

try {
    if ($IsAzureAutomation) {
        Write-Output "Connecting to Microsoft Graph using Managed Identity..."
        Connect-MgGraph -Identity -NoWelcome -ErrorAction Stop
    }
    else {
        Write-Information "Connecting to Microsoft Graph..." -InformationAction Continue
        $Scopes = @(
            "DeviceManagementConfiguration.Read.All"
        )
        Connect-MgGraphCommunity -Scopes $Scopes -NoWelcome -ErrorAction Stop
    }
    Write-Information "✓ Successfully connected to Microsoft Graph" -InformationAction Continue
}
catch {
    Write-Error "Failed to connect to Microsoft Graph: $($_.Exception.Message)"
    exit 1
}

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================

function Get-MgGraphAllPage {
    param(
        [string]$Uri,
        [int]$DelayMs = 100
    )

    $allResults = @()
    $nextLink = $Uri

    do {
        try {
            if ($allResults.Count -gt 0) {
                Start-Sleep -Milliseconds $DelayMs
            }

            $response = Invoke-MgGraphRequest -Uri $nextLink -Method GET

            if ($response.value) {
                $allResults += $response.value
            }
            else {
                $allResults += $response
            }

            $nextLink = $response.'@odata.nextLink'
        }
        catch {
            if ($_.Exception.Message -like "*429*") {
                Write-Information "Rate limit hit, waiting 60 seconds..." -InformationAction Continue
                Start-Sleep -Seconds 60
                continue
            }
            Write-Warning "Error fetching data: $($_.Exception.Message)"
            break
        }
    } while ($nextLink)

    return $allResults
}

function Get-SafeFileName {
    param([string]$Name)

    if ([string]::IsNullOrWhiteSpace($Name)) {
        return "unnamed"
    }

    $safe = $Name -replace '[\\/:*?"<>|]', '_'
    $safe = $safe.Trim().Trim('.')
    if ($safe.Length -gt 120) {
        $safe = $safe.Substring(0, 120)
    }
    if ([string]::IsNullOrWhiteSpace($safe)) {
        return "unnamed"
    }
    return $safe
}

function Export-BackupObject {
    param(
        [object]$InputObject,
        [string]$FolderPath,
        [string]$DisplayName,
        [string]$Id
    )

    $fileName = "$(Get-SafeFileName -Name $DisplayName)_$Id.json"
    $filePath = Join-Path $FolderPath $fileName
    $InputObject | ConvertTo-Json -Depth 25 | Out-File -FilePath $filePath -Encoding utf8
    return $fileName
}

# ============================================================================
# MAIN SCRIPT LOGIC
# ============================================================================

try {
    Write-Information "Starting Intune configuration backup..." -InformationAction Continue

    $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
    $backupRoot = Join-Path $OutputPath "IntuneConfigBackup_$timestamp"
    $null = New-Item -Path $backupRoot -ItemType Directory -Force

    $manifest = [ordered]@{
        backupDate    = (Get-Date -Format "o")
        areas         = @{}
        totalObjects  = 0
        backupVersion = "1.0"
    }

    # ----- Classic device configuration profiles -----
    if ($Areas -contains "DeviceConfigurations") {
        Write-Information "Exporting device configuration profiles..." -InformationAction Continue
        $folder = Join-Path $backupRoot "DeviceConfigurations"
        $null = New-Item -Path $folder -ItemType Directory -Force

        $profiles = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations?`$expand=assignments"
        foreach ($configProfile in $profiles) {
            $null = Export-BackupObject -InputObject $configProfile -FolderPath $folder -DisplayName $configProfile.displayName -Id $configProfile.id
        }

        $manifest.areas["DeviceConfigurations"] = @($profiles).Count
        $manifest.totalObjects += @($profiles).Count
        Write-Information "✓ Exported $(@($profiles).Count) device configuration profiles" -InformationAction Continue
    }

    # ----- Settings catalog policies -----
    if ($Areas -contains "SettingsCatalog") {
        Write-Information "Exporting settings catalog policies..." -InformationAction Continue
        $folder = Join-Path $backupRoot "SettingsCatalog"
        $null = New-Item -Path $folder -ItemType Directory -Force

        $policies = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies?`$expand=assignments"
        foreach ($policy in $policies) {
            # The list endpoint returns only a settingCount; the full setting bodies
            # live behind the per-policy settings navigation
            $settings = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies/$($policy.id)/settings"
            $policy | Add-Member -MemberType NoteProperty -Name "settings" -Value @($settings) -Force

            $null = Export-BackupObject -InputObject $policy -FolderPath $folder -DisplayName $policy.name -Id $policy.id
        }

        $manifest.areas["SettingsCatalog"] = @($policies).Count
        $manifest.totalObjects += @($policies).Count
        Write-Information "✓ Exported $(@($policies).Count) settings catalog policies" -InformationAction Continue
    }

    # ----- Compliance policies -----
    if ($Areas -contains "CompliancePolicies") {
        Write-Information "Exporting compliance policies..." -InformationAction Continue
        $folder = Join-Path $backupRoot "CompliancePolicies"
        $null = New-Item -Path $folder -ItemType Directory -Force

        # scheduledActionsForRule must be expanded explicitly; recreating a policy
        # without it is rejected by Graph, so the backup would be incomplete
        $compliancePolicies = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies?`$expand=assignments,scheduledActionsForRule(`$expand=scheduledActionConfigurations)"
        foreach ($policy in $compliancePolicies) {
            $null = Export-BackupObject -InputObject $policy -FolderPath $folder -DisplayName $policy.displayName -Id $policy.id
        }

        $manifest.areas["CompliancePolicies"] = @($compliancePolicies).Count
        $manifest.totalObjects += @($compliancePolicies).Count
        Write-Information "✓ Exported $(@($compliancePolicies).Count) compliance policies" -InformationAction Continue
    }

    # ----- Administrative template (ADMX) policies -----
    if ($Areas -contains "AdmxPolicies") {
        Write-Information "Exporting administrative template policies..." -InformationAction Continue
        $folder = Join-Path $backupRoot "AdmxPolicies"
        $null = New-Item -Path $folder -ItemType Directory -Force

        $admxPolicies = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations?`$expand=assignments"
        foreach ($policy in $admxPolicies) {
            # Definition values carry the actual configured settings; the expanded
            # definition gives human-readable names for the restore/report side
            $definitionValues = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations/$($policy.id)/definitionValues?`$expand=definition(`$select=id,classType,displayName,categoryPath),presentationValues"
            $policy | Add-Member -MemberType NoteProperty -Name "definitionValues" -Value @($definitionValues) -Force

            $null = Export-BackupObject -InputObject $policy -FolderPath $folder -DisplayName $policy.displayName -Id $policy.id
        }

        $manifest.areas["AdmxPolicies"] = @($admxPolicies).Count
        $manifest.totalObjects += @($admxPolicies).Count
        Write-Information "✓ Exported $(@($admxPolicies).Count) administrative template policies" -InformationAction Continue
    }

    # ----- Platform scripts (Windows PowerShell + macOS shell) -----
    if ($Areas -contains "PlatformScripts") {
        Write-Information "Exporting platform scripts..." -InformationAction Continue
        $folder = Join-Path $backupRoot "PlatformScripts"
        $null = New-Item -Path $folder -ItemType Directory -Force

        $scriptSurfaces = @(
            @{ Name = "deviceManagementScripts"; Label = "Windows PowerShell scripts" },
            @{ Name = "deviceShellScripts"; Label = "macOS shell scripts" }
        )

        $scriptCount = 0
        foreach ($surface in $scriptSurfaces) {
            $platformScripts = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/$($surface.Name)?`$expand=assignments"

            foreach ($platformScript in $platformScripts) {
                # scriptContent is always null on the list endpoint; the single-object
                # GET returns the base64 body
                if (-not $SkipScriptContent) {
                    try {
                        $detail = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/$($surface.Name)/$($platformScript.id)" -Method GET
                        $platformScript.scriptContent = $detail.scriptContent
                    }
                    catch {
                        Write-Warning "Could not fetch script content for '$($platformScript.displayName)': $($_.Exception.Message)"
                    }
                }

                $platformScript | Add-Member -MemberType NoteProperty -Name "scriptSurface" -Value $surface.Name -Force
                $null = Export-BackupObject -InputObject $platformScript -FolderPath $folder -DisplayName $platformScript.displayName -Id $platformScript.id
                $scriptCount++
            }

            Write-Information "✓ Exported $(@($platformScripts).Count) $($surface.Label)" -InformationAction Continue
        }

        $manifest.areas["PlatformScripts"] = $scriptCount
        $manifest.totalObjects += $scriptCount
    }

    # ----- Manifest -----
    $manifestPath = Join-Path $backupRoot "manifest.json"
    $manifest | ConvertTo-Json -Depth 5 | Out-File -FilePath $manifestPath -Encoding utf8

    Write-Information "`n========================================" -InformationAction Continue
    Write-Information "Backup Summary" -InformationAction Continue
    Write-Information "========================================" -InformationAction Continue
    foreach ($area in $manifest.areas.Keys) {
        Write-Information "$($area): $($manifest.areas[$area]) objects" -InformationAction Continue
    }
    Write-Information "Total: $($manifest.totalObjects) objects" -InformationAction Continue
    Write-Information "Backup folder: $backupRoot" -InformationAction Continue
    Write-Information "========================================" -InformationAction Continue
}
catch {
    Write-Error "Script execution failed: $($_.Exception.Message)"
    exit 1
}
finally {
    try {
        $null = Disconnect-MgGraph
        Write-Information "✓ Disconnected from Microsoft Graph" -InformationAction Continue
    }
    catch {
        Write-Verbose "Graph disconnection completed"
    }
}

// NOTES

Author notes

- Requires Microsoft.Graph.Authentication module - Uses beta Graph endpoints because the full Intune configuration surface is not exposed on v1.0 - Settings catalog setting bodies and ADMX definition values require one extra request per policy - Graph never returns secret values (encrypted OMA-URI settings, passwords, certificates) in exports; those settings appear with secret references only and must be re-entered manually after a restore - Local interactive sign-in uses the MgGraphCommunity module to avoid the Graph SDK's mandatory WAM broker on Windows

// RELATED

Picked by shared tags, category, and script type — nothing magic, just metadata overlap.

  1. Get Assignment Filter Audit

    This script retrieves all Intune assignment filters and cross-references them against every assignment surface that can carry a filter: device configuration profiles, settings catalog policies, compliance policies, administrative template policies, platform scripts, remediation scripts, and applications. It reports which filters are actually referenced by at least one assignment, which are unused, and which filters duplicate each other (same platform and same rule), so stale or redundant filters can be cleaned up safely.

    Configuration
  2. Get Assignment Matrix Report

    This script connects to Microsoft Graph and collects assignments across the major Intune surfaces: device configuration profiles, settings catalog policies, compliance policies, administrative template (ADMX) policies, platform scripts, remediation scripts, and applications. Every assignment is flattened into one row showing the target (group, all users, all devices, or exclusion), the resolved group name, the assignment filter with its mode, and the install intent for apps. The result answers "what does this group actually get" and "what targets this device population" in a single CSV.

    Configuration
  3. Get Group Assignments

    This script takes an Entra ID group (by display name or object ID) and scans all major Intune assignment surfaces to show exactly what that group receives: device configuration profiles, settings catalog policies, compliance policies, administrative template (ADMX) policies, platform scripts, remediation scripts, and applications with their install intents. Exclusion assignments are flagged, and tenant-wide All Users / All Devices assignments can be included to show the complete effective surface for members of the group.

    Configuration