The GroupPolicy module
The GroupPolicy module comes with GPMC (RSAT on Windows 11, the GPMC feature on servers). It works against domain GPOs through the PDC emulator by default (-Server picks another DC). It does not edit local policy; use LGPO.exe or secedit for that.
Import-Module GroupPolicy
Get-Command -Module GroupPolicy | Sort-Object Noun, Verb
Every cmdlet, grouped by job
| Job | Cmdlets |
|---|---|
| Find and read GPOs | Get-GPO, Get-GPOReport, Get-GPInheritance, Get-GPPermission, Get-GPStarterGPO |
| Create, rename, delete | New-GPO, Rename-GPO, Remove-GPO, New-GPStarterGPO, Copy-GPO |
| Links and inheritance | New-GPLink, Set-GPLink, Remove-GPLink, Set-GPInheritance |
| Edit registry policy settings | Get-GPRegistryValue, Set-GPRegistryValue, Remove-GPRegistryValue |
| Edit registry preference items | Get-GPPrefRegistryValue, Set-GPPrefRegistryValue, Remove-GPPrefRegistryValue |
| Permissions and filtering | Get-GPPermission, Set-GPPermission |
| Backup and restore | Backup-GPO, Restore-GPO, Import-GPO |
| Results and refresh | Get-GPResultantSetOfPolicy, Invoke-GPUpdate |
That is all 26 cmdlets (Get-GPPermission and Set-GPPermission also answer to the older plural names Get-GPPermissions and Set-GPPermissions). There are no cmdlets for security settings, WMI filters or most preference types; use GPMC, the backup XML, secedit templates imported through a backup, or the ActiveDirectory module for those.
Examine
# All GPOs with status, versions and dates
Get-GPO -All | Sort-Object DisplayName |
Select-Object DisplayName, Id, GpoStatus, CreationTime, ModificationTime,
@{n='UserVer';e={$_.User.DSVersion}}, @{n='CompVer';e={$_.Computer.DSVersion}}, WmiFilter
# One GPO's settings as HTML, or every GPO as one XML file
Get-GPOReport -Name "WS - Security Baseline" -ReportType Html -Path C:\reports\ws-baseline.html
Get-GPOReport -All -ReportType Xml -Path C:\reports\all-gpos.xml
# Which GPOs apply to an OU, in precedence order (includes inherited and enforced)
(Get-GPInheritance -Target "OU=Workstations,DC=corp,DC=contoso,DC=com").InheritedGpoLinks |
Select-Object Order, DisplayName, Enabled, Enforced, Target
# Find which GPO sets a registry-based policy value
Get-GPO -All | ForEach-Object {
$v = Get-GPRegistryValue -Guid $_.Id -Key 'HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate\AU' -ErrorAction SilentlyContinue
if ($v) { [pscustomobject]@{ GPO = $_.DisplayName; Values = ($v.ValueName -join ', ') } }
}
Where is each GPO linked (including Domain Controllers)
There is no Get-GPLink. Read the links from each GPO's XML report, or from the gPLink attribute of every container.
# Every GPO with all its links (from the GPO side)
Get-GPO -All | ForEach-Object {
[xml]$r = Get-GPOReport -Guid $_.Id -ReportType Xml
foreach ($l in @($r.GPO.LinksTo)) {
[pscustomobject]@{ GPO = $_.DisplayName; LinkedTo = $l.SOMPath; Enabled = $l.Enabled; Enforced = $l.NoOverride }
}
} | Sort-Object LinkedTo, GPO | Format-Table -AutoSize
# What is linked to the Domain Controllers OU, in order
$dcOu = (Get-ADDomain).DomainControllersContainer
(Get-GPInheritance -Target $dcOu).GpoLinks | Select-Object Order, DisplayName, Enabled, Enforced
# ...and everything that applies to DCs, including domain and site links
(Get-GPInheritance -Target $dcOu).InheritedGpoLinks | Select-Object Order, DisplayName, Target, Enforced
# Is any GPO linked to a site? (site links live in the configuration partition)
Get-ADObject -SearchBase (Get-ADRootDSE).ConfigurationNamingContext -LDAPFilter '(&(objectClass=site)(gPLink=*))' -Properties gPLink |
Select-Object Name, gPLink
Create, edit and link
# Create, comment and link a GPO; set link order 1
$gpo = New-GPO -Name "WS - Screen Lock" -Comment "Owner: EUC team. CHG1234."
$gpo.GpoStatus = 'UserSettingsDisabled' # computer-only GPO
New-GPLink -Guid $gpo.Id -Target "OU=Workstations,DC=corp,DC=contoso,DC=com" -LinkEnabled Yes -Order 1
# Registry-based policy (Administrative Template value). Use the key and value from the ADMX
# (look them up on registry.cloudschool.ai/policies/).
Set-GPRegistryValue -Guid $gpo.Id -Key 'HKLM\Software\Policies\Microsoft\Windows\Personalization' `
-ValueName 'NoLockScreenCamera' -Type DWord -Value 1
# -Disable stores a "delete this value" instruction: the client deletes the value at each refresh.
# (Not always the same as the ADMX "Disabled" state, which may write a value such as 0; check the policy.)
Set-GPRegistryValue -Guid $gpo.Id -Key 'HKLM\Software\Policies\Microsoft\Windows\Personalization' `
-ValueName 'NoLockScreenCamera' -Disable
# Registry preference item, removed when the GPO no longer applies
Set-GPPrefRegistryValue -Name "WS - Screen Lock" -Context Computer -Action Replace `
-Key 'HKLM\SOFTWARE\Contoso\Kiosk' -ValueName 'Mode' -Type String -Value 'Locked'
# Enforce, disable or reorder a link; block inheritance on an OU
Set-GPLink -Name "WS - Screen Lock" -Target "OU=Workstations,DC=corp,DC=contoso,DC=com" -Enforced Yes
Set-GPLink -Name "WS - Screen Lock" -Target "OU=Workstations,DC=corp,DC=contoso,DC=com" -LinkEnabled No
Set-GPInheritance -Target "OU=Kiosks,DC=corp,DC=contoso,DC=com" -IsBlocked Yes
# Security filtering
Set-GPPermission -Name "WS - Screen Lock" -TargetName "GG-Kiosk-PCs" -TargetType Group -PermissionLevel GpoApply
Set-GPPermission -Name "WS - Screen Lock" -TargetName "Authenticated Users" -TargetType Group -PermissionLevel GpoRead -Replace
# Rename, remove link, delete
Rename-GPO -Name "WS - Screen Lock" -TargetName "WS - Lock Screen"
Remove-GPLink -Name "WS - Lock Screen" -Target "OU=Workstations,DC=corp,DC=contoso,DC=com"
Remove-GPO -Name "WS - Lock Screen"
Set-GPRegistryValue accepts any key. A value outside the Policies branches will tattoo the registry like a preference, and the editor will show it under Extra Registry Settings. Use the exact key and value name from the vendor's ADMX.
Inventory and hygiene scripts
# Unlinked GPOs
Get-GPO -All | Where-Object {
([xml](Get-GPOReport -Guid $_.Id -ReportType Xml)).GPO.LinksTo -eq $null
} | Select-Object DisplayName, Id, ModificationTime
# Empty GPOs (no settings in either half)
Get-GPO -All | Where-Object { $_.User.DSVersion -eq 0 -and $_.Computer.DSVersion -eq 0 } | Select-Object DisplayName
# GPOs with all settings disabled
Get-GPO -All | Where-Object GpoStatus -eq 'AllSettingsDisabled' | Select-Object DisplayName
# GPOs nobody can apply (no Apply permission for anyone)
Get-GPO -All | Where-Object {
-not (Get-GPPermission -Guid $_.Id -All | Where-Object Permission -eq 'GpoApply')
} | Select-Object DisplayName
# AD vs SYSVOL version mismatch (run against each DC)
Get-GPO -All -Server DC01 | Where-Object {
$_.User.DSVersion -ne $_.User.SysvolVersion -or $_.Computer.DSVersion -ne $_.Computer.SysvolVersion
} | Select-Object DisplayName, @{n='User';e={"$($_.User.DSVersion)/$($_.User.SysvolVersion)"}},
@{n='Computer';e={"$($_.Computer.DSVersion)/$($_.Computer.SysvolVersion)"}}
# Passwords left in Group Policy Preferences (MS14-025)
Select-String -Path "\\$env:USERDNSDOMAIN\SYSVOL\$env:USERDNSDOMAIN\Policies\*\*\Preferences\*\*.xml" -Pattern 'cpassword="[^"]+' |
Select-Object Path
Troubleshoot
# RSoP for a remote user and computer
Get-GPResultantSetOfPolicy -Computer PC01 -User CORP\alice -ReportType Html -Path C:\pc01.html
# Refresh remotely now
Invoke-GPUpdate -Computer PC01 -Force -RandomDelayInMinutes 0
# Last Group Policy events on a machine
Get-WinEvent -ComputerName PC01 -LogName Microsoft-Windows-GroupPolicy/Operational -MaxEvents 50 |
Select-Object TimeCreated, Id, LevelDisplayName, Message
# Errors only, last 24 hours
Get-WinEvent -FilterHashtable @{ LogName='Microsoft-Windows-GroupPolicy/Operational'; Level=2; StartTime=(Get-Date).AddDays(-1) }
References: GroupPolicy module, Set-GPRegistryValue, Get-GPInheritance.