Skip to content
The Lazy Administrator
  • Home
  • Disclaimer
  • Contact
  • About Me
  • Search Icon

The Lazy Administrator

Finding ways to do the most work with the least effort possible

Backup and Restore your Intune Configuration using PowerShell

Backup and Restore your Intune Configuration using PowerShell

November 26, 2019 Brad Wyatt Comments 10 comments

Table of Contents

  • Pre-Requisites
    • AzureAD Module
    • MSGraphFunction Module
    • IntuneBackupandRestore Module
  • Connect to Microsoft Graph
  • Back Up Intune Configuration
  • Compare Intune Backup Files
  • Compare Intune Backup Directories
    • Parameters
    • Output
    • Example
    • Function
  • Restore Intune Backup
    • Restore Everything
    • Restore Single Backup File
    • Restore Assignments

I found this fantastic PowerShell module from jseerden that queries Microsoft Graph, and allows for cross-tenant Backup & Restore of your Intune Configuration. The Intune Configuration is backed up as JSON files in a given directory. The module allows an administrator to backup, restore and even compare different Intune backup sets.

Pre-Requisites

The module requires a total of 3 modules to be present, you can install them by running the following commands in an administrative PowerShell prompt:

AzureAD Module

  1. AzureAD (Install-Module -Name AzureAD)

MSGraphFunction Module

  1. MSGraphFunction (Install-Module -Name MSGraphFunctions)

IntuneBackupandRestore Module

The IntuneBackupandRestore PowerShell module is hosted on the PowerShell Gallery. You can install it by running the following command in an administrative PowerShell prompt:

Install-Module -Name IntuneBackupAndRestore

Connect to Microsoft Graph

Next, we will connect to Microsoft Graph using the “Microsoft Intune PowerShell” application in AzureAD, so you don’t need to create your own Azure AD application and set permissions. In PowerShell import the MSGraphFunctions module by running:

Import-Module MSGraphFunctions

Once you have the module loaded into memory, you will connect to Graph by running Connect-Graph. Connect-Graph loads a webform which supports MFA login. You will see a prompt asking for permissions, if you are a global administrator you can also consent on behalf of your organization.

Once authenticated, the shell will return to the normal prompt.

Note: The function Connect-Graph has a Credential parameter that accepts a PSCredential object if you wanted to automate the backup/restore.

Back Up Intune Configuration

To backup your tenant Intune configuration make sure you have the IntuneBackupandRestore module imported into memory. You can import it by running

Import-Module IntuneBackupandRestore

The Start-IntuneBackup cmdlet will then allow you to begin backing up your entire Intune configuration. The cmdlet has one parameter of FilePath. In my example I will save my Intune backup to $env:Temp\IntuneBackup folder. If the folder is not present it will automatically create the folder for you. To accomplish this I would run the command

Start-IntuneBackup -Path $Env:Temp\IntuneBackup

If I navigate to that folder in explorer I can see the backed up items from Intune. The files are backed up as JSON files, however if you got to \Device Management Scripts\Script Content you will see your Configuration PowerShell scripts which will be PS1 source files.

Compare Intune Backup Files

In an environment where multiple engineers may be making Intune changes, it may be beneficial to view configuration changes from a known working state to a later state. In my example, my ‘co worker’ made an Intune configuration change for the Bitlocker policy from my earlier Intune backup and forgot what he changed. To see what changed I took another backup and stored it at $Env:Temp\Intune2

To now compare both configuration I can use the Compare-IntuneBackupFile cmdlet. My full scriptblock would be

Compare-IntuneBackupFile -ReferenceFilePath "$Env:temp\IntuneBackup\Device Configurations\Bitlocker Encrypt.json" -DifferenceFilePath "$Env:temp\Intune2\Device Configurations\Bitlocker Encrypt.json"

In the Shell I can see that he changed the minimum pin length from 4 to 8

Compare Intune Backup Directories

This next function is something I created during this write up because I thought Compare-IntuneBackupFile would recursively list changes in two backup sets. Currently, I pushed this change to the master project on GitHub and its awaiting approval so until then you will have to load this function into memory separately. The function will recursively compare all the JSON backup files in each directory. Its able to match up the JSON files by looking for the reference JSON file in the backup directory, if there is more than 1 result (which you will see with things like assignments) it will go back to the reference file, extract its parent folder and compare it to the results.

Parameters

The function requires values for 2 parameters:

ReferenceDirectory: The directory containing the reference backup files

DifferenceDirectory: The directory containing the newest (difference) backup files

Output

Verbose messages are displayed if you use the -Verbose switch parameter. Otherwise you will just see files that are different and what is different between the two JSON files

Example

In my example I will compare 2 backup sets for changes. My first backup (reference) is located at $Env:Temp\IntuneBackup. My newest backup (Difference) is located at $Env:\Intune2. My example command would be

Compare-IntuneBackupDirectories -Verbose -ReferenceDirectory C:\Users\BRADWY~1\AppData\Local\Temp\IntuneBackup -DifferenceDirectory C:\Users\BRADWY~1\AppData\Local\Temp\Intune2

Function

Below is the PowerShell function. Until it becomes approved and part of the module you will have to load it into memory. I was using ISE so I just ran it in ISE (or VSCode). You could also dot source the file. It does require the IntuneBackupAndRestore module to be installed and loaded into memory.

Function Compare-IntuneBackupDirectories
{
	<#
    .SYNOPSIS
    Compare two Intune Backup Directories for changes in each of their JSON backup files.
    
    .DESCRIPTION
    Compare two Intune Backup Directories for changes.
    
    .PARAMETER $ReferenceDirectory
    Any Intune Backup Directory.
    
    .PARAMETER $DifferenceDirectory
    Latest Intune Backup directory
    
    .EXAMPLE
	- Show verbose output
    Compare-IntuneBackupDirectories -Verbose -ReferenceDirectory C:\Users\BradleyWyatt\AppData\Local\Temp\IntuneBackup -DifferenceDirectory C:\Users\BradleyWyatt\AppData\Local\Temp\IntuneNewBackup
	
	Compare-IntuneBackupDirectories -ReferenceDirectory C:\Users\BradleyWyatt\AppData\Local\Temp\IntuneBackup -DifferenceDirectory C:\Users\BradleyWyatt\AppData\Local\Temp\IntuneNewBackup
    
    .NOTES
    Requires the IntuneBackupAndRestore Module
	
	.AUTHOR
	Bradley Wyatt - The Lazy Administrator
    #>
	
	Param (
		[parameter(Mandatory = $true, Position = 0)]
		[String]$ReferenceDirectory,
		[parameter(Mandatory = $true, Position = 1)]
		[String]$DifferenceDirectory
		
	)
	Begin
	{
		$ReferenceFiles = Get-ChildItem $ReferenceDirectory -Recurse | Where-Object { $_.Name -like "*.json*" } | Select-Object -ExpandProperty VersionInfo
		
		$DifferenceFiles = Get-ChildItem $DifferenceDirectory -Recurse | Where-Object { $_.Name -like "*.json*" } | Select-Object @{ Label = "FileName"; Expression = { (($_.VersionInfo).FileName).split("\") | Select-Object -Last 1 } }, @{ Label = "FullPath"; Expression = { (($_.VersionInfo).FileName) } }
	}
	Process
	{
		Foreach ($File in $ReferenceFiles)
		{
			$ReferenceJSONFile = ($File.Filename).split("\") | Select-Object -last 1
			
			Write-Verbose "The reference file is '$ReferenceJSONFile'"
			Write-Verbose "The reference file path is $($File.FileName)"
			
			$DifFileFound = $DifferenceFiles | Where-Object { $_.FileName -eq $ReferenceJSONFile }
			
			If (($DifFileFound.FileName).count -gt 1)
			{
				$ReferenceJSONFile = ($File.Filename).split("\") | Select-Object -last 2
				$ReferenceJSONFileParent = ($File.FileName).split("\") | Select-Object -Last 2
				$ReferenceJSONFileParentPath = "$(($ReferenceJSONFileParent).item(0))\$(($ReferenceJSONFileParent).item(1))"
				Write-Verbose "Multiple difference files found that were matching the reference file"
				$DifFileFound = $DifferenceFiles | Where-Object { $_.FullPath -like "*$ReferenceJSONFileParentPath*" }
			}
			
			Write-Verbose "The difference file is located at $($DifFileFound.fullpath)"
			
			Write-Verbose "Checking for changes in the file '$ReferenceJSONFile'"
			
			$Changes = Compare-IntuneBackupFile -ReferenceFilePath $File.FileName -DifferenceFilePath $DifFileFound.FullPath -ErrorAction silentlycontinue
			If ($Changes)
			{
				Write-Host "There was a change in the file, '$ReferenceJSONFile' which is located at $($DifFileFound.fullpath)"
				$Changes | Format-Table -AutoSize
			}
		}
	}
}

Restore Intune Backup

In this next section we will go over restoring Intune configuration and assignments.

Note: Restoring configurations will NOT overwrite existing configurations, but instead create new ones. Restoring assignments may overwrite existing assignments

Restore Everything

To restore everything you would use the Start-IntuneRestoreConfig cmdlet. It will recursively parse the folder structure and restore everything from each JSON file. It requires value for the Path parameter which is the directory of the backup files.

Start-IntuneRestoreConfig -Path C:\Users\BRADWY~1\AppData\Local\Temp\IntuneBackup\

 

Restore Single Backup File

To restore a single backup file you would use the same command as above. It will again parse the backup folder structure and import any JSON files it finds so to only backup 1 JSON file you will need to ensure its the only JSON file in all of the directories. If you delete the directories you will see some red on your console because it cannot find a folder its looking for but the import will still work. In my example I just went through all of the folders and deleted the JSON files I didn’t want to import.

Restore Assignments

To restore Intune Assignments you will use the Start-IntuneRestoreAssignments cmdlet. It will recursively parse your assignments and check to see if it needs to be restored or hasn’t changed. In my example only Adobe Reader had an assignment change and it was successfully changed back.

Brad Wyatt
Brad Wyatt

My name is Bradley Wyatt; I am a 5x Microsoft Most Valuable Professional (MVP) in Microsoft Azure and Microsoft 365. I have given talks at many different conferences, user groups, and companies throughout the United States, ranging from PowerShell to DevOps Security best practices, and I am the 2022 North American Outstanding Contribution to the Microsoft Community winner.


Azure, Intune
Automation, Azure, Graph, Intune, PowerShell

Post navigation

PREVIOUS
Office 365 Email Address Policies with Azure Automation
NEXT
Homeland Security’s Trusted Travelers API and PowerShell – Getting a Better Global Entry Interview Using PowerShell

10 thoughts on “Backup and Restore your Intune Configuration using PowerShell”

  1. Jason Moore says:
    November 26, 2019 at 9:24 am

    I enjoy reading all your articles, thanks for all the hard work!

    Reply
  2. Bernhard Köck says:
    January 31, 2020 at 12:52 pm

    Wow, that’s so cool. Works perfect. Thanks a lot.

    Reply
  3. Lacy says:
    June 24, 2020 at 4:03 pm

    Yeah, I have to say it worked out for me as well. Cool man!

    Reply
  4. Lacy says:
    June 24, 2020 at 4:03 pm

    Yeah, I have to say it worked out for me as well. Cool man! Do you mind if i link back to your content here?

    Reply
    1. Brad Wyatt says:
      June 30, 2020 at 8:48 am

      not at all!!

      Reply
  5. Jesper says:
    January 15, 2021 at 8:27 am

    Can this be used to automatic setup new intune tenants by backing up one with all configurations and then restore it to another?

    Reply
    1. Brad Wyatt says:
      January 18, 2021 at 1:53 pm

      Yes I believe that will work

      Reply
    2. Charlie says:
      January 24, 2024 at 2:42 pm

      Did you end up trying this is the end? Was it possible?

      Reply
  6. Robert Bosnjak says:
    September 22, 2021 at 2:44 am

    Hi Brad,
    is there something similar for backing up Azure Configurations? For example Azure resource groups, vm networks, vm´s etc.
    Regards Robert

    Reply
  7. Huw Weatherhead says:
    April 25, 2023 at 5:34 am

    Didn’t twig until I tried this on a freshly installed machine, but I believe Microsoft.graph.intune is also a pre-requisite.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe

Email


Categories

  • Active Directory (8)
  • AI (3)
  • API (1)
  • AutoPilot (2)
  • Azure (15)
  • Bicep (4)
  • Connectwise (1)
  • Defender for Cloud Apps (1)
  • Delegated Admin (1)
  • DevOps (6)
  • Graph (6)
  • Intune (15)
  • LabTech (1)
  • Microsoft Teams (6)
  • Office 365 (19)
  • Permissions (2)
  • PowerShell (50)
  • Security (1)
  • SharePoint (3)
  • Skype for Business (1)
  • Terraform (1)
  • Uncategorized (2)
  • Yammer (1)

Recent Comments

  • Kristopher Gates on Getting Started with GitHub Copilot in the CLI
  • MD SHARIQUE AKHTAR on Modern Active Directory – An update to PSHTML-AD-Report
  • TommyBoich on How The ConnectWise Manage API Handles Pagination with PowerShell
  • LOTTERY 365 LOGIN on Windows LAPS Management, Configuration and Troubleshooting Using Microsoft Intune
  • SPRUNKI PHASE 6 on Get a New Computer’s Auto Pilot Hash Without Going Through the Out of Box Experience (OOBE)

1,739,418 People Reached

© 2025   All Rights Reserved.