{"id":4687,"date":"2012-11-12T00:01:00","date_gmt":"2012-11-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/11\/12\/force-a-domain-wide-update-of-group-policy-with-powershell\/"},"modified":"2012-11-12T00:01:00","modified_gmt":"2012-11-12T00:01:00","slug":"force-a-domain-wide-update-of-group-policy-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/force-a-domain-wide-update-of-group-policy-with-powershell\/","title":{"rendered":"Force a Domain-Wide Update of Group Policy with PowerShell"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, shows how to force a domain-wide update of Group Policy by using Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well, tomorrow, the Scripting Wife and I leave for a three-week European Windows PowerShell tour. We will be doing five Windows PowerShell user groups and meeting with over a dozen Windows PowerShell MVPs, and other people from the Windows PowerShell community. I will be making extra blog postings about the trip, so stay tuned for details. If you are anywhere near where we will be, I encourage you to catch up with us. <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/hh182567\" target=\"_blank\">The Scripting Guys community page<\/a> has all the details. We will also be making postings on <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a>.<\/p>\n<h2>Updating Group Policy&mdash;domain-wide<\/h2>\n<p>Occasionally, I make a change to Group Policy on the network, and I want to force the policy to update on all the computers. Occasionally, they make changes at work, and I need to update my local Group Policy settings.<\/p>\n<p>To update Group Policy settings, I use the GPUpdate<em> <\/em>utility. The GPUpdate utility has a number of switches. By default, GPUpdate updates both computer and user portions of Group Policy. But, I can control that by using the <strong>\/target<\/strong> parameter. For example, if I only want to update the computer portion of the policy, I use the \/target:computer. To update the user portion, it is \/target:user. The following command shows this technique.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; gpupdate \/target:computer<\/p>\n<p style=\"padding-left: 30px\">Updating policy&#8230;<\/p>\n<p style=\"padding-left: 30px\">Computer Policy update has completed successfully.<\/p>\n<p>By default, GPUpdate will update only modified Group Policy settings. If I want to update all settings, use the <strong>\/force<\/strong> parameter. The command shown here updates all settings (regardless if they are modified) for both the computer and the user portions of Group Policy.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; gpupdate \/force<\/p>\n<p style=\"padding-left: 30px\">Updating policy&#8230;<\/p>\n<p style=\"padding-left: 30px\">Computer Policy update has completed successfully.<\/p>\n<p style=\"padding-left: 30px\">User Policy update has completed successfully.<\/p>\n<h2>First, collect the computers in the domain<\/h2>\n<p>The first thing I need to do is to obtain a collection of all the computers on the domain. To do this, I use the <strong>Get-ADComputer<\/strong> cmdlet from the Active Directory module.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;The Active Directory module is available from the RSAT tools.<\/p>\n<p>I store the returned computer objects in a variable named $cn. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">$cn = Get-ADComputer -filt *<\/p>\n<h2>Second, create remote sessions<\/h2>\n<p>The second thing I need to do is to create a remote session with all the remote computers. To do this, I need to supply credentials to use for the remote session as well as use the <strong>New-PSSession<\/strong> cmdlet to create the connection. The first part is easy, I use the <strong>Get-Credential<\/strong> cmdlet and store the returned credential object. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential iammred\\administrator<\/p>\n<p>Now, I use the <strong>New-PSSession<\/strong> cmdlet. I provide the names of the computers and the credentials. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">$session = New-PSSession -cn $cn.name -cred $cred<\/p>\n<p>One thing to keep in mind is that at any given time in my domain, there are computers that are offline. These return as errors. Windows PowerShell keeps on creating new sessions in spite of the errors appearing in the console. The command and associated errors are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0654.hsg-11-12-12-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0654.hsg-11-12-12-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The presence of lots of errors may be disturbing. Because the returned session objects reside in the <strong>$session<\/strong> variable, I can easily check to ensure the sessions are open. This is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2664.hsg-11-12-12-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2664.hsg-11-12-12-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Now, run the command on all the remote machines<\/h2>\n<p>To run the GPUpdate command on all my remote machines, I use the <strong>Invoke-Command<\/strong> cmdlet. The cool thing is that it uses the session objects stored in the <strong>$session<\/strong> variable. <strong>ICM<\/strong> is an alias for the <strong>Invoke-Command<\/strong> cmdlet. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">icm -Session $session -ScriptBlock {gpupdate \/force}<\/p>\n<p>When I run the command, the results are shown in the Windows PowerShell console.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7723.hsg-11-12-12-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7723.hsg-11-12-12-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Check to see if the update worked properly<\/h2>\n<p>When Group Policy settings successfully apply to a workstation, an event 1502 is displayed in the System event log on the computer. I can easily use <strong>Invoke-Command<\/strong> to retrieve this information. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">icm -Session $session -ScriptBlock {Get-EventLog -LogName system -InstanceId1502 -Newest 1}<\/p>\n<p>The command and associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8400.hsg-11-12-12-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8400.hsg-11-12-12-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>A final fun Group Policy thing &hellip;<\/h2>\n<p>At times, I have to call the Help desk at work, and the answer is to refresh Group Policy on my local computer. This is not a problem because I can run GPUpdate from within Windows PowerShell. The thing is that they often want me to update Group Policy five times, and then wait five minutes between refreshes. No problem&mdash;I can do that in a single Windows PowerShell command line. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">1..5 | % {&#8220;refreshing GP $(Get-Date)&#8221;; gpupdate \/force ; sleep 300}<\/p>\n<p>The output and command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0167.hsg-11-12-12-05.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0167.hsg-11-12-12-05.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to force a domain-wide update of Group Policy by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Well, tomorrow, the Scripting Wife and I leave for a three-week European Windows PowerShell tour. We will be doing five Windows PowerShell user groups and meeting with over [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,305,152,3,20,45],"class_list":["post-4687","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computers","tag-group-policy","tag-scripting-guy","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to force a domain-wide update of Group Policy by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Well, tomorrow, the Scripting Wife and I leave for a three-week European Windows PowerShell tour. We will be doing five Windows PowerShell user groups and meeting with over [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4687"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4687\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}