System.GroupPolicy API Reference
The System.GroupPolicy namespace provides classes that enable developers to interact with Windows Group Policy objects, read and write policy settings, and manage Resultant Set of Policy (RSoP) data.
Contents
Overview
The System.GroupPolicy namespace contains types that represent Group Policy objects (GPOs), security filtering, WMI filters, and more. It is primarily used by system administrators and developers creating custom management tools.
Classes
GroupPolicyObject– Represents a single GPO.GroupPolicyObjectCollection– Collection ofGroupPolicyObjectinstances.ResultantSetOfPolicy– Provides methods to query RSoP data.GroupPolicyException– Exception type for Group Policy errors.
Key Methods
| Class | Method | Description |
|---|---|---|
GroupPolicyObject | Load(string gpoName) | Loads a GPO by its display name. |
GroupPolicyObject | GetSetting(string key) | Retrieves a specific policy setting. |
GroupPolicyObjectCollection | GetAll() | Returns all GPOs applied to the current computer. |
ResultantSetOfPolicy | Query(string targetComputer) | Runs an RSoP query against a target machine. |
Code Examples
// Requires reference to System.GroupPolicy.dll
using System;
using System.GroupPolicy;
class Program
{
static void Main()
{
var gpoCollection = new GroupPolicyObjectCollection();
foreach (var gpo in gpoCollection.GetAll())
{
Console.WriteLine($"Name: {gpo.Name}, Id: {gpo.Id}");
}
}
}
Import-Module GroupPolicy
$gpo = Get-GPO -Name "Default Domain Policy"
$setting = $gpo | Get-GPRegistryValue -Key "HKLM\Software\Policies\Microsoft\Windows\System" -ValueName "EnableLUA"
Write-Output "EnableLUA = $($setting.Value)"
Remarks
The System.GroupPolicy namespace is available starting with .NET Framework 4.6 and .NET 5+. It requires the client machine to run Windows 10/Server 2016 or later and to have the Group Policy Management Console (GPMC) installed.
For remote queries, ensure that the account executing the code has sufficient permissions (Domain Administrator or delegated GPO read rights).