Wednesday, 14 August 2013

Remote Group Policy update using gpupdate in C#

You can update or refresh the GPO easily by the command line utility tool gpupdate. By using  following  commands we can update GPO on local machine.

 gpupdate /force -update user and computer configuration
 gpupdate /target:computer /force -update only computer configuration
 gpupdate /target:user /force -update only user configuration

Remote Group Policy update in C#

You can use the below C# function to update GPO on remote machine.
Note: For local machine don't use username,password and Impersonation

private static void UpdateGPO(string machinename)
                ConnectionOptions connectionOptions = new ConnectionOptions();

                connectionOptions.Username = @"Domain\Administrator";
                connectionOptions.Password = "password";
                connectionOptions.Impersonation = ImpersonationLevel.Impersonate;

                ManagementScope scope = new ManagementScope("\\\\" + machinename + "\\root\\CIMV2", connectionOptions);


                ManagementClass clas = new ManagementClass(scope, new ManagementPath("Win32_Process"), new ObjectGetOptions());

                ManagementBaseObject inparams = clas.GetMethodParameters("Create");

                inparams["CommandLine"] = "GPUpdate /force";

                ManagementBaseObject outparam = clas.InvokeMethod("Create", inparams, null);
            catch (Exception ex)


Local Group Policy update in C#

You can use the below C# function to update GPO on local machine.

private static void UpdateGPO()
      Process proc = new Process();
      ProcessStartInfo procStartInfo = new ProcessStartInfo(@"cmd.exe","/c"+"gpupdate/force");
      procStartInfo.RedirectStandardOutput = true;
      procStartInfo.UseShellExecute = false;
      procStartInfo.CreateNoWindow = true;
      procStartInfo.LoadUserProfile = true;
      proc.StartInfo = procStartInfo;
   catch (Exception ex)


Related Articles:

Active Directory Attribute mapping with Friendly name - user
Active Directory Search Filter Examples
Create new Active Directory User in C#
How to get list of all domain controllers in C#
Restore a deleted Active Directory object using C#
Active Directory search filter by ObjectGuid 

Software Developer


No comments:

Post a Comment