{"id":2720,"date":"2016-03-07T09:02:53","date_gmt":"2016-03-07T07:02:53","guid":{"rendered":"http:\/\/9v.lt\/blog\/?p=2720"},"modified":"2022-01-19T08:34:37","modified_gmt":"2022-01-19T06:34:37","slug":"separating-ad-users-groups","status":"publish","type":"post","link":"http:\/\/9v.lt\/blog\/separating-ad-users-groups\/","title":{"rendered":"Separating AD users into groups"},"content":{"rendered":"<p>In our AD we have many users, all of them are unified in one general mailgroup &#8220;visi&#8221;. One day I was tasked to split users into english and lithuanian groups, so that emails in my language wouldn&#8217;t bother those who can&#8217;t read them :P<br \/>\nIt was easy with my newly acquired Powershell skill, and we were dividing people into groups by their departments, so I knew the english speaking departments, all I had to do was list out all the people and add them to &#8220;visilt&#8221; and &#8220;visien&#8221; groups according by their departments.<br \/>\n<!--more--><br \/>\n<\/p>\n<pre lang=\"powershell\">\r\nImport-module ActiveDirectory\r\n\r\n$users = Get-ADUser -Filter {(Enabled -eq \"True\")} -SearchBase \"OU=Folder,DC=subdomain,DC=domain,DC=lt\" -Server subdomain -Properties Name, MemberOf, DistinguishedName | Select Name, MemberOf, DistinguishedName\r\nforeach ($user in $users) {\r\n    if (($user.Name -notlike \"0*\") -and ($user.Name -notlike \"WTF*\")) {\r\n        if (($user.MemberOf -notcontains \"CN=visien,OU=MailGrupes,DC=subdomain,DC=domain,DC=lt\") -and (($user.MemberOf -like \"CN=F-*\") -or ($user.MemberOf -like \"CN=E-*\"))) {\r\n            # jeigu jis yra uzsienietis (grupeje F-* and E-*) tai dedam ji i visien\r\n            Write-Host \"Ne Lietuvis: $($user.Name)\"\r\n            Add-ADGroupMember -Identity \"CN=visien,OU=MailGrupes,DC=subdomain,DC=domain,DC=lt\" -Member $user.DistinguishedName -Server subdomain -ErrorAction SilentlyContinue\r\n            # Write-Host \"-------------------------\"\r\n            # exit\r\n        } elseif (($user.MemberOf -notcontains \"CN=visien,OU=MailGrupes,DC=subdomain,DC=domain,DC=lt\") -and ($user.MemberOf -notcontains \"CN=visilt,OU=MailGrupes,DC=subdomain,DC=domain,DC=lt\")) {\r\n            # jeigu jis ne uzsienietis ir nera grupeje visilt, tai dedam i ta grupe\r\n            Write-Host \"Lietuvis: $($user.Name)\"\r\n            Add-ADGroupMember \"CN=visilt,OU=MailGrupes,DC=subdomain,DC=domain,DC=lt\" -Member $user.DistinguishedName -Server subdomain -ErrorAction SilentlyContinue\r\n            # Write-Host \"-------------------------\"\r\n            # exit\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In our AD we have many users, all of them are unified in one general<\/p>\n","protected":false},"author":2,"featured_media":2721,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,750],"tags":[952,951,949],"class_list":["post-2720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","category-software-projects","tag-activedirectory","tag-ad","tag-powershell"],"_links":{"self":[{"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/2720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/comments?post=2720"}],"version-history":[{"count":0,"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/2720\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media\/2721"}],"wp:attachment":[{"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media?parent=2720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/categories?post=2720"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/tags?post=2720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}