Search code examples
c#.netactive-directorywmigroup-policy

How to programatically update group policy? i.e. gpupdate /force


I have several local accounts which are created at install time with C#. There is a group policy that in turn grants certain permissions to these new accounts.

The problem I am trying to solve is how do I go about getting the group policy pushed to the new accounts. Without the group policy applied the application will not function.

Opening a cmd prompt and running gpupdate /force fixes it, but I need a more seamless transition between install time and run time.


Solution

  • That should do the trick:

        private void UpdateGroupPolicy()
        {
            FileInfo execFile = new FileInfo("gpupdate.exe");
            Process proc = new Process();
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.StartInfo.FileName = execFile.Name;
            proc.StartInfo.Arguments = "/force";
            proc.Start();
            //Wait for GPUpdate to finish
            while (!proc.HasExited)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
            MessageBox.Show("Update procedure has finished");
        }