i am the funny
 

 I AM THE FUNNY!!!

Ok, so somtimes it is just easier to create a custom object when you are getting info. An example might be when you want everything to show up in one table rather then multiple in a foreach loop.

So, lets break it down...

$myobj = "" | Select "DisplayName","extensionAttribute2","extensionAttribute3","HomeDirectory","l","StreetAddress","mail"

This is setting the properties and the object. So, you can tell that I am basically getting everthing from an AD query on a user account, but you can call the properties what ever you want. They do not need to match up with where you get them from... I just found it easier to remember that way.

The next step is just to add info into each property. Now, if you change the name of a property in the first line, make sure you change it in the populating section as well. I can not tell you how many times data has not been returned when I run the script because I forgot to do it...

$myobj.DisplayName = $info.DisplayName
$myobj.extensionAttribute2 = $info.extensionAttribute2
$myobj.extensionAttribute3 = $info.extensionAttribute3
$myobj.HomeDirectory = $info.HomeDirectory
$myobj.l = $info.l
$myobj.StreetAddress = $info.StreetAddress
$myobj.mail = $info.mail

Ok, so you have your object... so now what? Well, depends on where you are using the object. As I am doing this in a foreach loop, I will add it to an array, which I have defined outside of the loop.

$Output += $myobj

So, the script will look in its entirety:

Import-Module ActiveDirectory

# I included how to manually write in the usernames or import from a list because of reasons...
#$ArrUser = "user1","user2","user3","user4","user5","user6"
$ArrUser = Get-Content -Path C:\TEMP\userlist.txt

$Output = @()

foreach ($user in $ArrUser){
    $info = Get-ADUser -identity $user -properties *
    write-host $info.HomeDirectory
    
    $myobj = "" | Select "DisplayName","extensionAttribute2","extensionAttribute3","HomeDirectory","l","StreetAddress","mail"

    $myobj.DisplayName = $info.DisplayName
    $myobj.extensionAttribute2 = $info.extensionAttribute2
    $myobj.extensionAttribute3 = $info.extensionAttribute3
    $myobj.HomeDirectory = $info.HomeDirectory
    $myobj.l = $info.l
    $myobj.StreetAddress = $info.StreetAddress
    $myobj.mail = $info.mail

    $Output += $myobj

    $myobj = $null

}

$Output | Export-Csv -Path C:\TEMP\userdata.csv