Exporting Usernames and Domain

This topic contains 4 replies, has 5 voices, and was last updated by Profile photo of kvprasoon kvprasoon 1 year, 3 months ago.

  • Author
  • #37836
    Profile photo of Marc Simpson
    Marc Simpson

    Hi all

    Am trying to export a list of usernames from a security group, the users in this group are spread over multiple domains. – The following command gets me a list of usernames but what would i need to add in to get which domain the user is on too?

    Get-ADGroupMember -identity “GROUPNAME” | select SamAccountName | Export-csv -path C:\Output\Groupmembers.csv -NoTypeInformation


  • #37843
    Profile photo of Martin Lhotsky
    Martin Lhotsky

    Hi Marc,

    If you can rely on Active Directory domain name for your domains being the same as DNS domain then you can use following:

    function Get-CompanyDomain {
        param (
        $directory_dn_parts = $DirectoryDN.ToLower() -split "dc="
        $dns_domain = ''
        for ($loop = 1; $loop -lt $directory_dn_parts.Length; $loop ++) {
            $dns_domain += $directory_dn_parts[$loop].Replace(',','') + '.'
        ### $netbios_domain = Insert NetBIOS domain information if needed
        $company_domain_property = [ordered] @{
            'DNSDomain' = $dns_domain
            'NetBIOSDomain' = $netbios_domain
        $company_domain = New-Object -TypeName PSObject -Property $company_domain_property

    Your export line then will look like this:

    Get-ADGroupMember -identity “GROUPNAME” | select sAMAccountName,@{Name='Domain';Expression={(Get-CompanyDomain -DirectoryDN $PSItem.distinguishedName).DNSDomain}}

    Hope this helps

  • #37844
    Profile photo of AK

    DistinguishedName or CanonicalName would give you an idea which domain this user belongs to.

  • #38176
    Profile photo of Peter Johnson
    Peter Johnson

    Hey Marc

    If you can download and install the Quest AD Cmdlets from software.dell.com try this:

    get-qadgroupmember -identity "x" | get-qaduser | Select DisplayName,NTAccountName,Domain,SamAccountName | export-csv -notypeinformation -delimiter ";" -path xmembers.csv -append.

    I've just tried it in my customers prod forest. works 100%

  • #38202
    Profile photo of kvprasoon

    Hi Marc

    Hopes This Info Helps You .

    $GMDetails = Get-ADGroupMember $GoupName | select-object -property SamAccountName,distinguishedName 
    Foreach($G in $GMDetails){$DN = $G.distinguishedName -replace ',DC=','.'
    	$C = ($DN.Split('.')).count -1
    	$DN -match "(\.[a-z]+){$C}"
    	$matches[0]|foreach-object -process{
    	New-Object -TypeName PSObject -Property @{

You must be logged in to reply to this topic.