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
  • 5
    Have you tried turning it off and on again? – Scott Pack Mar 1 '13 at 14:32
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.