I'm trying to write a script that gets the predefined templates that I've created in Microsoft 365 admin center. The problem is that I'm getting users list as templates, and not the templates. Is there a way to get those templates and not the users as templates?
the script:
# PowerShell Script to Retrieve User Information in Microsoft 365
# Ensure you have the required Microsoft Graph PowerShell modules installed
# Install-Module -Name Microsoft.Graph -Scope CurrentUser -Force
# Import required Microsoft Graph modules
Import-Module Microsoft.Graph.Users
Import-Module Microsoft.Graph.Identity.DirectoryManagement
Import-Module Microsoft.Graph.Authentication
# Connect to Microsoft Graph with appropriate permissions
Connect-MgGraph -Scopes "User.Read.All", "Directory.Read.All", "Organization.Read.All"
# Function to retrieve user creation configurations
function Get-UserCreationDetails {
try {
# Retrieve anization details
$ = Get-MgOrganization
Write-Host "Organization Details:" -ForegroundColor Green
Write-Host "Tenant ID: $($.Id)" -ForegroundColor Cyan
Write-Host "Display Name: $($.DisplayName)" -ForegroundColor Cyan
Write-Host "Default Domain: $($.VerifiedDomains[0].Name)" -ForegroundColor Cyan
# Retrieve user creation default settings
Write-Host "`nUser Creation Default Attributes:" -ForegroundColor Green
$defaultUserTemplate = @{
"UsageLocation" = $.PreferredLanguage
"Country" = $.CountryLetterCode
"DefaultDomain" = $.VerifiedDomains[0].Name
}
$defaultUserTemplate | Format-Table -AutoSize
# Retrieve available user attributes
Write-Host "`nAvailable User Attributes:" -ForegroundColor Green
$userAttributes = Get-MgUser -Top 1 | Select-Object -First 1 | Get-Member |
Where-Object {$_.MemberType -eq 'Property' -and $_.Name -notlike '*Internal*'} |
Select-Object Name | Sort-Object Name
$userAttributes | Format-Table -AutoSize
# Retrieve user creation policies (if available)
Write-Host "`nUser Creation Policies:" -ForegroundColor Green
$policies = Get-MgPolicyAuthorizationPolicy
if ($policies) {
$policies | Format-List
}
else {
Write-Host "No specific user creation policies found." -ForegroundColor Yellow
}
}
catch {
Write-Host "Error retrieving user details: $_" -ForegroundColor Red
}
}
# Function to list user templates or default configurations
function Get-UserTemplateConfigurations {
try {
# Retrieve user template configurations
Write-Host "Searching for User Template Configurations..." -ForegroundColor Cyan
# Get initial user template attributes
$userTemplates = Get-MgUser -All |
Select-Object -First 10 |
Select-Object DisplayName, UserPrincipalName, Mail, JobTitle, Department
if ($userTemplates) {
Write-Host "Sample User Templates:" -ForegroundColor Green
$userTemplates | Format-Table -AutoSize
}
else {
Write-Host "No user templates found." -ForegroundColor Yellow
}
}
catch {
Write-Host "Error retrieving user templates: $_" -ForegroundColor Red
}
}
# Main execution
Write-Host "Retrieving Microsoft 365 User Creation Details..." -ForegroundColor Cyan
Get-UserCreationDetails
Get-UserTemplateConfigurations
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Result:
Thank for the help.