All scripts
Automation 105

Backup-EntraNamedLocations

Exports every Conditional Access named location (trusted networks, country lists) to JSON, so location definitions can be restored or diffed after a change.

Backup-EntraNamedLocations.ps1
<#
.SYNOPSIS
    Backs up Conditional Access named locations to JSON.

.DESCRIPTION
    Exports every named location, both IP range and country based, to a
    timestamped JSON file, so location definitions used across
    Conditional Access policies can be restored or diffed after a
    change.

.PARAMETER BackupPath
    Folder to write the backup file to. Defaults to the current directory.

.EXAMPLE
    .\Backup-EntraNamedLocations.ps1

.NOTES
    Requires Microsoft.Graph.Identity.SignIns with an active
    Connect-MgGraph session.

.AUTHOR
    Shehryar Hassan
#>

param(
    [string]$BackupPath = (Get-Location).Path
)

$locations = Get-MgIdentityConditionalAccessNamedLocation -All

$file = Join-Path $BackupPath "entra-named-locations_$(Get-Date -Format yyyyMMdd_HHmmss).json"
$locations | ConvertTo-Json -Depth 6 | Out-File -FilePath $file -Encoding utf8
Write-Host "Backed up $($locations.Count) named location(s) to $file" -ForegroundColor Cyan

Read it before you run it, and test in a safe tenant first.