# Extract USers BAsed on SamAccountname (Secondary Accounts)

**URL:** <https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245>\
**Category:** PowerShell Help\
**Created:** [April 24, 2020, 8:40am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245 "2020-04-24T08:40:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfirita](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@sfirita](https://forums.powershell.org/u/sfirita)\
**Post date:** [April 24, 2020, 8:40am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/1 "2020-04-24T08:40:07Z")

</div>

hello everyone,

i’m new to this forums, i discovred it few days ago, so much helpful people arround here 🙂

i’m new to powershell and wanted a guidance from the professionals,

we have in our little production environment all users with unique codes, and separate account for Admins sharing the same Code with a “\_admin” added, so for example :

-Normal user account : USER1 , respective Admin Account : USER1\_admin

&nbsp;

i wan to extract and export all users’s SamAccountNames with their respective admin SAN accounts, and i’m kind of lost. can anyone illuminate me about a way to achieve this ?

&nbsp;

thanking you all in advance !

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [April 24, 2020, 9:23am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/2 "2020-04-24T09:23:14Z")

</div>

```
Get-ADuser | sort samAccountName
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 24, 2020, 9:26am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/3 "2020-04-24T09:26:43Z")

</div>

sfirita, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did).

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

> [@](#):
>
> … and i’m kind of lost.

What exactly is it what you don’t understand? Please show your code - even if it does not work as you expect it or does not work at all.

---

<div class="post-metadata">

**Author:** ![sfirita](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@sfirita](https://forums.powershell.org/u/sfirita)\
**Post date:** [April 24, 2020, 9:39am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/4 "2020-04-24T09:39:05Z")

</div>

[quote quote=222189]sfirita, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did).

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

> [sfirita12 wrote:](https://powershell.org/forums/topic/extract-users-based-on-samaccountname-secondary-accounts/)
> … and i’m kind of lost.

What exactly is it what you don’t understand? Please show your code – even if it does not work as you expect it or does not work at all.[/quote]

hello again,

thank you for your answer, yeah i know i didnt ask for a complete script 🙂

just wanted a way out that i can follow, i want to learn , and to do that i have to fail and retry 🙂

so this is my script, which is working fine to extract usersnwith selected attributes :

```auto
get-aduser -Server $Server -Filter * -SearchBase "OU=GROUPADMIN,OU=COUNTRY,OU=User-Accounts,DC=EM,DC=cab" -Properties Enabled, SamAccountName, createTimeStamp, Lastlogon, AccountExpires, DisplayName, LockedOut, Description, sn ,
givenName , CanonicalName, mail, Department, physicalDeliveryOfficeName, telephoneNumber, city, memberof |

select enabled, SamAccountName, createtimestamp, @{Name=”LastLogon”;Expression={[DateTime]::FromFileTime($_.lastlogon)}}, @{Name=”accountexpires”;Expression={[DateTime]::FromFileTime($_.accountexpires)}},
DisplayName, LockedOut, Description, sn , givenName , CanonicalName, mail, Department, physicalDeliveryOfficeName, telephoneNumber, city, @{Label="DA";Expression={If($_.MemberOf -join "" -match "CN=Domain Admins,"){"Yes"}Else{"No"}}} | Out-GridView -OutputMode Single

```

what i want is a way to add another column, where based on the samaccountname i can have the sameaccountnames with a “\_a” tag in the end, cause as i explained, we have different admin accounts for the same SAN , differentiated with the “\_a” . i hope i explained well my problem, english is not my native language.

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [April 24, 2020, 10:17am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/5 "2020-04-24T10:17:30Z")

</div>

```
# Let's not do ugly please :)
$ParameterList = @{
    Server = $Server 
    Filter = '*' 
    SearchBase = 'OU=GROUPADMIN,OU=COUNTRY,OU=User-Accounts,DC=EM,DC=cab'
    Properties = @('Enabled','SamAccountName','createTimeStamp','Lastlogon','AccountExpires',
        'DisplayName','LockedOut','Description','sn ','givenName','CanonicalName','mail',
        'Department','physicalDeliveryOfficeName','telephoneNumber','city','memberof')
}

$PropertyList = @('enabled','SamAccountName','createtimestamp',
    @{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.lastlogon)}},
    @{Name='accountexpires';Expression={[DateTime]::FromFileTime($_.accountexpires)}},
    'DisplayName','LockedOut','Description','sn','givenName','CanonicalName','mail','Department',
    'physicalDeliveryOfficeName','telephoneNumber','city',
    @{Label='DA';Expression={If($_.MemberOf -join '' -match 'CN=Domain Admins,'){'Yes'}Else{'No'}}}
)

$UserList = Get-ADUser @ParameterList | select $PropertyList 
# $UserList | Out-GridView -OutputMode Single

$AdminList = $UserList | where SamAccountName -Match '_admin'
$NotAdminList = $UserList | where SamAccountName -NotMatch '_admin'
$NotAdminList | Add-Member -MemberType NoteProperty -Name 'AdminSamAccountName' -Value '' -EA 0 
$NotAdminList | foreach {
    $_.AdminSamAccountName = ($AdminList | where where SamAccountName -Match $_.SamAccountName).SamAccountName
}
$NotAdminList | Out-GridView -OutputMode Single
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 24, 2020, 10:35am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/6 "2020-04-24T10:35:33Z")

</div>

Another approach if you don’t need tons of properties from the admin account:

```
$users = Get-ADUser -Filter * |
         Select Name,
                SamAccountName,
                @{Name='Admin';Expression={Get-ADUser -Filter "SamAccountName -eq 'admin-$_.SamAccountName'"}}
```

Not tested, but something like this should be possible.

---

<div class="post-metadata">

**Author:** ![sfirita](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@sfirita](https://forums.powershell.org/u/sfirita)\
**Post date:** [April 24, 2020, 11:00am UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/7 "2020-04-24T11:00:12Z")

</div>

thanks everyone for your help, i’ve got some ideas, i’ll try this out, and will let you know 🙂 thans a lot again !

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/extract-users-based-on-samaccountname-secondary-accounts/14245/8 "2024-05-16T20:29:51Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


