How to Configure Password Protection with Microsoft Graph

A lesser-known capability of Microsoft Graph is the ability to view, monitor, and modify an organisation’s Microsoft Entra ID password protection policies from the command line. These settings are often part of security benchmarks, such as the CIS Benchmark for Microsoft 365, making it important to be able to view and monitor them programmatically.

In this article, I’ll show you how to use Microsoft Graph PowerShell to view and create the following policies in Microsoft Entra: Entra ID > Authentication Methods > Password Protection.

Password Protection Microsoft Entra
Password Protection Microsoft Entra

View current Password Protection settings with Microsoft Graph

To view the current Password Protection settings in Microsoft Entra ID using Microsoft Graph PowerShell, you can use the following commands:

Connect-MgGraph -Scopes Directory.Read.All

$uri = "beta/settings"
$Settings = Invoke-MgGraphRequest -Uri $uri -OutputType PSObject | Select -expand value

($Settings | Where {$_.templateId -eq "5cf42378-d67d-4f36-ba46-e8b86229381d"}).values

If the commands return no output, it likely means that the Password Protection settings have not been modified in your tenant and remain at their default, disabled state.

If a response is returned, it will resemble the following output:

Password Protection settings response
Password Protection settings response

You can use these values to determine if these settings are in their correct or recommended state, or whether they need to be modified.

Creating new Password Protection settings for the first time

As mentioned, if the previous commands returned no output, a Password Protection policy does not yet exist in your tenant. You must first create one from the system template (template ID 5cf42378-d67d-4f36-ba46-e8b86229381d).

To do this, use the following hashtable to define the policy settings, including your custom list of banned passwords, then run the script to create the settings.

Connect-MgGraph -Scopes Directory.ReadWrite.All

$uri = "beta/settings"

$params = @{
    templateId = "5cf42378-d67d-4f36-ba46-e8b86229381d"
    values = @(
        @{
            name = "EnableBannedPasswordCheck"
            value = "True"
        }
        @{
            name = "BannedPasswordList"
            value = @("Daniel","OCNetwork") -join "`t"
        }
        @{
            name = "LockoutDurationInSeconds"
            value = "600"
        }
        @{
            name = "LockoutThreshold"
            value = "10"
        }
        @{
            name = "EnableBannedPasswordCheckOnPremises"
            value = "True"
        }
        @{
            name = "BannedPasswordCheckOnPremisesMode"
            value = "Audit"
        }
    )
}
  
Invoke-MgGraphRequest -Uri $uri -Method POST -Body $params

If successful, you should get a simple response containing the policy settings you just created, like so.

Creating Password Protection settings response

Update Password Protection settings with Microsoft Graph PowerShell

To modify the Password Protection settings, all you need to do is obtain the directory object ID and include that in the URI of the request, then, using the same body as the previous command, modify your settings and send the reqeust as a PATCH request, like so.

Connect-MgGraph -Scopes Directory.ReadWrite.All

$uri = "beta/settings"
$Settings = Invoke-MgGraphRequest -Uri $uri -OutputType PSObject | Select -expand value
$policy = $Settings | Where {$_.templateId -eq "5cf42378-d67d-4f36-ba46-e8b86229381d"}

$params = @{
    values = @(
        @{
            name = "EnableBannedPasswordCheck"
            value = "True"
        }
        @{
            name = "BannedPasswordList"
            value = @("Daniel","OCNetwork") -join "`t"
        }
        @{
            name = "LockoutDurationInSeconds"
            value = "600"
        }
        @{
            name = "LockoutThreshold"
            value = "10"
        }
        @{
            name = "EnableBannedPasswordCheckOnPremises"
            value = "False"
        }
        @{
            name = "BannedPasswordCheckOnPremisesMode"
            value = "Audit"
        }
    )
}
  
Invoke-MgGraphRequest -Uri "$uri/$($policy.id)" `
-Method PATCH `
-Body $params

Also, if you wanted to provide an updated banned password list by providing a CSV of passwords, you can use the example below:

$passwords = (import-csv -path "G:\passwords.csv").passwords -join "`t"

$params = @{
    values = @(
        @{
            name = "EnableBannedPasswordCheck"
            value = "True"
        }
        @{
            name = "BannedPasswordList"
            value = $passwords
        }
        @{
            name = "LockoutDurationInSeconds"
            value = "600"
        }
        @{
            name = "LockoutThreshold"
            value = "10"
        }
        @{
            name = "EnableBannedPasswordCheckOnPremises"
            value = "False"
        }
        @{
            name = "BannedPasswordCheckOnPremisesMode"
            value = "Audit"
        }
    )
}
  
Invoke-MgGraphRequest -Uri "$uri/$($policy.id)" `
-Method PATCH `
-Body $params

Daniel Bradley

Daniel Bradley is a UK-based technical architect and Microsoft MVP in Security. He specialises in Microsoft Entra, Intune, and Microsoft Graph PowerShell and is the author of Microsoft Graph PowerShell for Administrators. He writes and edits Our Cloud Network using experience gained from working with Microsoft cloud environments.

Leave a Reply