3

I'm trying to enumerate the name, samaccountname, and domain that each user in my forest belongs to and write it to a text file.

The script that I have now is:

Import-Module ActiveDirectory

$domains = "root.org", "child1.root.org", "child2.root.org"


ForEach ($d in $domains){

Get-ADUser -Filter * -ResultSetSize $null -Server $d -Properties name, samaccountname |
Select-Object name, samaccountname | out-file c:\users\mdmarra\desktop\users.txt -append

}

What I need is the value of $d at the end of each line as well so that the output looks like

name          samaccountname        domain
----          --------------        ------
Marra,Mark    mdmarra               root.org
1
  • 5
    Have you tried turning it off and on again?
    – Scott Pack
    Mar 1, 2013 at 14:32

1 Answer 1

7

That's something you can easily achieve with hashtables in Select-Object:

ForEach ($d in $domains){

Get-ADUser -Filter * -ResultSetSize $null -Server $d -Properties name, samaccountname |
Select-Object name, samaccountname, @{ Name = 'domain'; Expression = { $d }} | out-file c:\users\mdmarra\desktop\users.txt -append

}
1
  • Yep, that's perfect.
    – MDMarra
    Mar 1, 2013 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .