How to access/Update Group Policy Objects using C sharp

2024-05-08T07:35:50.82+00:00

Hi,

We are referring below link to access group policies programmatically   using c sharp.

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmi_v2/class-library/gpo-class-microsoft-grouppolicy

This is sample code which we have written to access GPO Object. We are able to access the object successfully but not able to access the policies .

We are aiming to access the polices which comes under computer configuration and user configuration.

Specifically we want to update Account Policies ,Local Policies.

GPDomain _gpDomain = new GPDomain();   Gpo gpoTarget = _gpDomain.GetGpo(gpo.DisplayName);   ComputerConfiguration computer = gpoTarget.Computer;   PolicySettings policySettings = computer.Policy;   PolicyRegistrySetting policyRegistrySetting = new PolicyRegistrySetting();   RegistryPolicy registryPolicy = policySettings.GetRegistry(false); //string report= gpoTarget.GenerateReport(ReportType.Xml);   // Get the type of the PolicySettings object   Type policySettingsType = policySettings.GetType();   Type policySettingsType1 = registryPolicy.GetType();

GPPermissionCollection gPPermissions= gpoTarget.GetSecurityInfo();

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,355 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 34,831 Reputation points Microsoft Vendor
    2024-05-08T12:30:22.03+00:00

    Hi @Thakur, Apurva [EMR/SYSS/PSS/PUNE] , Welcome to Microsoft Q&A,

    You seem to be overlooking the critical steps of accessing and updating the policy. In your code, you have successfully obtained the GPO object and the computer configuration, but you need to further access the policy object under the computer configuration and update it. Likewise, you need to do the same to access policies under User Configuration. Try the following code:

    GPDomain _gpDomain = new GPDomain();
    GPO gpoTarget = _gpDomain.GetGPO(gpo.DisplayName);
    
    //Access policies under computer configuration
    PolicySettings computerPolicySettings = gpoTarget.Computer.Policy;
    
    //Access policies under user configuration
    PolicySettings userPolicySettings = gpoTarget.User.Policy;
    
    // Example of updating local policy
    RegistryPolicy computerRegistryPolicy = computerPolicySettings.GetRegistry(true);
    RegistryPolicy userRegistryPolicy = userPolicySettings.GetRegistry(true);
    
    // Add or update registry policy under computer configuration
    RegistryPolicySetting computerRegistryPolicySetting = new RegistryPolicySetting("PathToRegistryKey", "RegistryValueName", "RegistryValueData");
    computerRegistryPolicy.Set(computerRegistryPolicySetting);
    
    // Add or update registry policy under user configuration
    RegistryPolicySetting userRegistryPolicySetting = new RegistryPolicySetting("PathToRegistryKey", "RegistryValueName", "RegistryValueData");
    userRegistryPolicy.Set(userRegistryPolicySetting);
    
    // save Changes
    gpoTarget.Save();
    
    // Generate report
    string report = gpoTarget.GenerateReport(ReportType.Xml);
    
    // Get permission information
    GPPermissionCollection gPPermissions = gpoTarget.GetSecurityInfo();
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.