Public/TenantConfiguration/Baseline/Add-NamedLocations.ps1

function Add-NamedLocations {
    param(
        [Parameter(Mandatory)]
        [string]$TenantId
    )

    try {
        Connect-CustomerGraph -CustomerTenantId $TenantId

        $NamedLocations = Get-MgIdentityConditionalAccessNamedLocation -All

        $NamedLocationFiles = Get-ChildItem -Path "$PSScriptRoot\NamedLocations" -Filter *.json
        foreach($NamedLocationFile in $NamedLocationFiles) {
            $NamedLocation = Get-Content -Path $NamedLocationFile.FullName | ConvertFrom-Json -Depth 100 -AsHashtable
            if($NamedLocations.DisplayName -contains $NamedLocation.DisplayName) {
                Write-Host "Named location '$($NamedLocation.DisplayName)' already exists, not creating.." -ForegroundColor Yellow
            }
            else {
                $NamedLocation = New-MgIdentityConditionalAccessNamedLocation -BodyParameter $NamedLocation
                Write-Host "Created named location '$($NamedLocation.DisplayName)'!" -ForegroundColor Green
            }
        }
    }
    catch {
        throw "Failed to create named locations: $_"
    }
}