{"id":51473,"date":"2010-01-27T00:01:00","date_gmt":"2010-01-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/01\/27\/hey-scripting-guy-can-i-use-windows-powershell-to-manage-ad-ds-security-groups\/"},"modified":"2010-01-27T00:01:00","modified_gmt":"2010-01-27T00:01:00","slug":"hey-scripting-guy-can-i-use-windows-powershell-to-manage-ad-ds-security-groups","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-windows-powershell-to-manage-ad-ds-security-groups\/","title":{"rendered":"Hey, Scripting Guy! Can I Use Windows PowerShell to Manage AD DS Security Groups?"},"content":{"rendered":"<p><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/p>\n<p>&nbsp;\n<img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I am a MCSE and I still remember my instructor at the training center running around the room shouting, &ldquo;Users go into groups, and groups get assigned rights and permissions.&rdquo; Over and over again in different manners and in different language, but always with the same intent, he kept repeating the same mantra. I could not get that phrase out of my head, which was a good thing when it came time to take the exam. My network is therefore organized in such a way that I make extensive use of security groups. My question is this: Is there a way I can use Windows PowerShell to create and manage security groups in Active Directory Domain Services (AD DS)?<\/p>\n<p class=\"MsoNormal\">&#8212; DM<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello DM, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I was up late last night reading <a href=\"http:\/\/en.wikipedia.org\/wiki\/Odyssey\"><font face=\"Segoe\">Homer&#8217;s Odyssey<\/font><\/a>, and therefore I did not spring from my slumber with my normal Scripting Guy exuberance. I therefore added an extra scoop of English Breakfast tea leaves to my pot before heading up to the Scripting Guy command center. There were several cool tweets on Twitter that needed responses, and with Deep Purple cranked up so loud on my Zune HD that the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Venetian_blinds\"><font face=\"Segoe\">Venetian blinds<\/font><\/a> were actually rattling, I dove into the <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> inbox. <\/p>\n<p class=\"MsoNormal\">Your mail brought back fond memories of Cincinnati where I used to teach the class for the Microsoft NT 4.0 Server in the Enterprise Exam 70-068. (I contributed to a study guide for that exam&mdash;one of my first projects as a writer.) Speaking of Cincinnati, I found the following picture of the Cincinnati <a href=\"http:\/\/en.wikipedia.org\/wiki\/Tyler_Davidson_Fountain\"><font face=\"Segoe\">Tyler Davidson Fountain<\/font><\/a> that I took a few years ago when I was teaching a VBScript class at the Microsoft Office there.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Tyler Davidson Fountain\" alt=\"Image of Tyler Davidson Fountain\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0127\/hsg-01-27-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">DM, let us now get started. To create a new global security group, use the <b>New-ADGroup<\/b> Windows PowerShell AD DS cmdlet. The <b>New-ADGroup<\/b> Windows PowerShell cmdlet requires three parameters: the name of the group, a path to the location where the group will be stored, and the <b>groupscope<\/b> (<b>global<\/b>, <b>universal<\/b>, or <b>domainlocal<\/b>). Before running the command seen here, remember you must import the <b>ActiveDirectory<\/b> module into your current Windows PowerShell session. For more information about working with the <b>ActiveDirectory<\/b> module, see <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/01\/25\/hey-scripting-guy-january-25-2010.aspx\"><font face=\"Segoe\">Monday&rsquo;s Hey, Scripting Guy! Blog post<\/font><\/a>.<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">New-ADGroup -Name hsgTestGroup -Path &#8220;ou=HSG_TestOU,dc=nwtraders,dc=com&#8221; -groupScope global<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The newly created group is seen in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of newly created group\" alt=\"Image of newly created group\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0127\/hsg-01-27-10-02.jpg\" width=\"400\" height=\"442\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">To create a new universal group, you only need to change the <b>groupscope<\/b> parameter value, as seen here. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">New-ADGroup -Name hsgTestGroup1 -Path &#8220;ou=HSG_TestOU,dc=nwtraders,dc=com&#8221; -groupScope universal<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The newly created universal group is seen in Active Directory Users and Computers, as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of newly create universal group\" alt=\"Image of newly create universal group\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0127\/hsg-01-27-10-03.jpg\" width=\"600\" height=\"405\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">To add a user to a group, you must supply values for the <b>identity<\/b> parameter and the <b>members<\/b> parameter. The value you use for the <b>identity<\/b> parameter is the name of the group. You do not need to use the LDAP syntax of <b>cn=groupname<\/b><i>.<\/i> You need only to supply the name. In examining the LDAP attributes for a group in ADSI Edit, as seen in the following image, you can obtain the needed value from several fields. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of LDAP attributes for a group in ADSI Edit\" alt=\"Image of LDAP attributes for a group in ADSI Edit\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0127\/hsg-01-27-10-04.jpg\" width=\"400\" height=\"445\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">It is a bit unusual that the <b>-members<\/b> parameter is\nnamed <b>members<\/b> and not <b>member<\/b> because most Windows PowerShell cmdlet parameter names are singular and not plural. The parameters are singular even when they accept an array of values (such as the <b>computername<\/b> parameter). The command to add a new group named <b>hsgTestGroup1<\/b> to the <b>hsgUserGroupTest<\/b> group is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Add-ADGroupMember -Identity hsgTestGroup1 -Members hsgUserGroupTest<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To remove a user from a group, use the <b>Remove-ADGroupMember<\/b> cmdlet with the name of the user and group. The <b>identity<\/b> and <b>members<\/b> parameters are required, but the command will not execute without confirmation, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Remove-ADGroupMember -Identity hsgTestGroup1 -Members hsgUserGroupTest<\/p>\n<p>Confirm<br \/>Are you sure you want to perform this action?<br \/>Performing operation &#8220;Set&#8221; on Target &#8220;CN=hsgTestGroup1,OU=HSG_TestOU,DC=NWTraders,DC=Com&#8221;.<br \/>[Y] Yes<span>&nbsp; <\/span>[A] Yes to All<span>&nbsp; <\/span>[N] No<span>&nbsp; <\/span>[L] No to All<span>&nbsp; <\/span>[S] Suspend<span>&nbsp; <\/span>[?] Help (default is &#8220;Y&#8221;): y<br \/>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If you are sure that you wish to remove the user from the group and you wish to suppress the query, use the <b>&ndash;confirm<\/b> parameter and assign the value <b>$false<\/b> to it. The problem is you will need to supply a colon between the parameter and <b>$false<\/b> value. <\/p>\n<p class=\"Readeraidonly\">The use of the colon before the <b>&ndash;confirm<\/b> parameter is not documented, and took me more than two hours of experimentation to figure out. I also did extensive searches on <a href=\"http:\/\/www.bing.com\/\"><font face=\"Segoe\">Bing<\/font><\/a> and was unable to find anything. <\/p>\n<p class=\"MsoNormal\">The command is seen here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Remove-ADGroupMember -Identity hsgTestGroup1 -Members hsgUserGroupTest -Confirm:$false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You need the ability to suppress the confirmation prompt to be able to use the <b>Remove-ADGroupMember<\/b> cmdlet in a script. The first thing the RemoveUserFromGroup.ps1 script does is load the <b>activedirectory<\/b> module. After the module is loaded, the <b>Remove-ADGroupMember<\/b> cmdlet is used to remove the user from the group. To suppress the confirmation prompt, the <b>&ndash;confirm:$false<\/b> command is used. The RemoveUserFromGroup.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>RemoveUserFromGroup.ps1<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">import-module activedirectory<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Remove-ADGroupMember -Identity hsgTestGroup1 -Members hsgUserGroupTest -Confirm:$false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">DM, that is all there is to working with Groups in Active Directory. Active Directory Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">FaceBook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> or post them on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I am a MCSE and I still remember my instructor at the training center running around the room shouting, &ldquo;Users go into groups, and groups get assigned rights and permissions.&rdquo; Over and over again in different manners and in different language, but always with the same intent, he kept repeating the [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,44,3,45],"class_list":["post-51473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-groups","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I am a MCSE and I still remember my instructor at the training center running around the room shouting, &ldquo;Users go into groups, and groups get assigned rights and permissions.&rdquo; Over and over again in different manners and in different language, but always with the same intent, he kept repeating the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51473","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51473"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51473\/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=51473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}