Exporting Usernames and Domain

Welcome Forums General PowerShell Q&A Exporting Usernames and Domain

This topic contains 4 replies, has 5 voices, and was last updated by

2 years, 6 months ago.

  • Author
  • #37836

    Points: 0
    Rank: Member

    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

    Points: 0
    Rank: Member

    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

    Points: 14
    Rank: Member

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

  • #38176

    Points: 0
    Rank: Member

    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

    Points: 587
    Helping Hand
    Rank: Major Contributor

    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 @{

The topic ‘Exporting Usernames and Domain’ is closed to new replies.