{"id":1271,"date":"2012-06-25T11:46:45","date_gmt":"2012-06-25T10:46:45","guid":{"rendered":"http:\/\/www.edwardsd.co.uk\/work\/?p=1271"},"modified":"2013-10-02T09:23:21","modified_gmt":"2013-10-02T08:23:21","slug":"list-domain-users-domainmembers-ps1","status":"publish","type":"post","link":"https:\/\/www.edwardsd.co.uk\/work\/2012\/06\/list-domain-users-domainmembers-ps1\/","title":{"rendered":"List Domain Users using Script | PowerShell"},"content":{"rendered":"<p>This is custom made code to output all users listed in Active Directory.<br \/>\nThis code was rewritten using Powershell in order to make the output more clear and &#8220;visually pleasing&#8221;<\/p>\n<p><strong>Powershell Option (output to looks like this):<\/strong><\/p>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">ngg_shortcode_0_placeholder<\/span><\/p>\n<p><strong>To use<\/strong>:<\/p>\n<ol>\n<li>Paste into notepad.<\/li>\n<li>Save as &#8220;DomainMembers.ps1&#8221;<\/li>\n<li>Run with Powershell (no progress will be displayed)<\/li>\n<li>Data result will be output to &#8220;DomainMembers.txt&#8221; located in the C:\\DomainMembers.txt This can be specified under the varible &#8216;$path = &#8220;C:\\DomainMember.txt&#8221;&#8216;<\/li>\n<\/ol>\n<p><strong>Note:<\/strong>Sometimes the script will not run correctly and the following will be displayed:<br \/>\n<em>&#8220;File cannot be loaded because the execution of scripts is disabled on this system&#8221;<\/em><br \/>\nThis security can be removed using the following command.<\/p>\n<pre lang=\"powershell\">Set-ExecutionPolicy Unrestricted -Force<\/pre>\n<p><!--more--><\/p>\n<pre lang=\"powershell\">#********************************************************************\r\n#*\r\n#* File: DomainMembers.ps1\r\n#* Created: June 2012\r\n#* Version: v2.1\r\n#* Copyright (C): (Richard Deeming)\r\n#*\r\n#* Main Function: Listing All The User Related Infomations In The Domain.\r\n#*\r\n#********************************************************************\r\nfunction Find-DefaultNamingContext()\r\n{\r\n\t$root = New-Object System.DirectoryServices.DirectoryEntry(\"LDAP:\/\/rootDSE\")\r\n\tif ($null -eq $root) { return null }\r\n\treturn $root.Properties[\"defaultNamingContext\"].Value\r\n}\r\n\r\nfunction Convert-SearchResult($result, $propertyMap)\r\n{\r\n\t$item = New-Object PSObject\r\n\r\n\tforeach ($pair in $propertyMap.GetEnumerator())\r\n\t{\r\n\t\t$name = $pair.Name\r\n\t\t$realName = $pair.Value\r\n\t\t$value = \"\"\r\n\r\n\t\t$prop = $result.Properties[$name.ToLower()]\r\n\t\tif (($null -ne $prop) -and (0 -ne $prop.Count))\r\n\t\t{\r\n\t\t\tif (1 -eq $prop.Count)\r\n\t\t\t{\r\n\t\t\t\t$value = $prop[0]\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\t$value = $prop | select-object\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tAdd-Member -InputObject $item -MemberType NoteProperty -Name $realName -Value $value\r\n\t}\r\n\r\n\treturn $item\r\n}\r\n\r\nfunction List-DirectoryEntries([string] $rootPath, [string] $filter, [array] $propertiesToLoad, $propertyMap)\r\n{\r\n\t$fullPath = \"LDAP:\/\/$rootPath\"\r\n\r\n\t$root = New-Object System.DirectoryServices.DirectoryEntry($fullPath)\r\n\t$searcher = New-Object System.DirectoryServices.DirectorySearcher($root, $filter, $propertiesToLoad)\r\n\r\n\t$searcher.PageSize = 10\r\n\t$searcher.SizeLimit = 10000\r\n\r\n\t$results = $searcher.FindAll()\r\n\r\n\tforeach ($result in $results){\r\n\t\t$item = Convert-SearchResult $result $propertyMap\r\n\t\t$item | select-object\r\n\t}\r\n}\r\n\r\nfunction List-Computers([string] $rootPath = \"\")\r\n{\r\n\tif (0 -eq $rootPath.Length) { $rootPath = Find-DefaultNamingContext }\r\n\r\n\t$filter = \"(objectCategory=computer)\"\r\n\r\n\t$propertiesToLoad = @(\"name\")\r\n\t$propertyMap = @{ \"name\" = \"Name\" }\r\n\r\n\tList-DirectoryEntries $rootPath $filter $propertiesToLoad $propertyMap\r\n}\r\n\r\nfunction List-Users([string] $rootPath = \"\", [string] $groupName = \"\")\r\n{\r\n\tif (0 -eq $rootPath.Length) { $rootPath = Find-DefaultNamingContext }\r\n\r\n\tif (0 -eq $groupName.Length)\r\n\t{\r\n\t\t$filter = \"(&amp;(objectCategory=person)(objectClass=user))\"\r\n\t}\r\n\telse\r\n\t{\r\n\t\t$filter = \"(&amp;(objectCategory=person)(objectClass=user)(memberOf=$groupName))\"\r\n\t}\r\n\r\n\t$propertiesToLoad = @(\"samAccountName\", \"displayName\", \"description\")\r\n\t$propertyMap = @{\r\n\t\t\"samAccountName\" = \"Username\"\r\n\t\t\"displayName\" = \"DisplayName\"\r\n\t\t\"description\" = \"Description\"\r\n\t}\r\n\r\n\tList-DirectoryEntries $rootPath $filter $propertiesToLoad $propertyMap\r\n}\r\n\r\nfunction List-Groups([string] $rootPath = \"\")\r\n{\r\n\tif (0 -eq $rootPath.Length) { $rootPath = Find-DefaultNamingContext }\r\n\r\n\t$fullPath = \"LDAP:\/\/$rootPath\"\r\n\t$filter = \"(objectCategory=group)\"\r\n\t$propertiesToLoad = @(\"distinguishedname\", \"name\")\r\n\r\n\t$root = New-Object System.DirectoryServices.DirectoryEntry($fullPath)\r\n\t$searcher = New-Object System.DirectoryServices.DirectorySearcher($root, $filter, $propertiesToLoad)\r\n\r\n\t$searcher.PageSize = 10\r\n\t$searcher.SizeLimit = 10000\r\n\r\n\t$results = $searcher.FindAll()\r\n\r\n\tforeach ($result in $results){\r\n\r\n\t\t$cn = $result.Properties[\"distinguishedname\"][0]\r\n\t\t$name = $result.Properties[\"name\"][0]\r\n\t\t$children = List-Users $rootPath $cn\r\n\r\n\t\t$properties = @{\r\n\t\t\tGroupName = $name\r\n\t\t\tMembers = $children\r\n\t\t}\r\n\r\n\t\tNew-Object PSObject -Property $properties\r\n\t}\r\n}\r\n\r\nfunction Get-DomainInfo([string] $rootPath = \"\")\r\n{\r\n\tif (0 -eq $rootPath.Length) { $rootPath = Find-DefaultNamingContext }\r\n\r\n\tWrite-Output \"Computers:\"\r\n\tWrite-Output \"----------\"\r\n\tList-Computers $rootPath | sort-object Name | fw -property Name -column 1\r\n\tWrite-Output \"\"\r\n\r\n\tWrite-Output \"Users:\"\r\n\tWrite-Output \"------\"\r\n\tList-Users $rootPath | sort-object Username | ft Username, DisplayName, Description -auto\r\n\tWrite-Output \"\"\r\n\r\n\tWrite-Output \"Groups:\"\r\n\tWrite-Output \"-------\"\r\n\r\n\t$groups = List-Groups $rootPath | where-object { $_.Members -ne $null } | sort-object GroupName\r\n\tforeach ($group in $groups)\r\n\t{\r\n\t\tWrite-Output \"$($group.GroupName):\"\r\n\t\t$group.Members | sort-object Username | ft Username, DisplayName -auto\r\n\t}\r\n}\r\n$path = \"C:\\DomainMembers.txt\"\r\nif(!(Test-Path -Path $path))\r\n  {\r\n   New-Item $path -type file\r\n  }\r\nelse\r\n  {\r\n   Clear-Content $path\r\n  }\r\nGet-DomainInfo | Out-String | Add-Content $path<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is custom made code to output all users listed in Active Directory. This code was rewritten using Powershell in order to make the output more clear and &#8220;visually pleasing&#8221; Powershell Option (output to looks like this): To use: Paste into notepad. Save as &#8220;DomainMembers.ps1&#8221; Run with Powershell (no progress will be displayed) Data result [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[21,30,23],"tags":[],"class_list":["post-1271","post","type-post","status-publish","format-standard","hentry","category-code","category-powershell","category-windows-server-2008"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pOPt8-kv","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/posts\/1271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/comments?post=1271"}],"version-history":[{"count":0,"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/posts\/1271\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/media?parent=1271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/categories?post=1271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edwardsd.co.uk\/work\/wp-json\/wp\/v2\/tags?post=1271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}