![]() |
I AM THE FUNNY!!! |
---|
Ok, let's talk about table formatting. So, when you are piping a table out onto the screen, a lot of the time you would just do something like...
write-host $emails | Format-Table -AutoSize
While this works, sometimes you might want to be more specific with how you want the table formatted, for whatever reason, maybe you like more space? Or one of the attributes is just ridiculously long!
Anyway, this page is just a little bit of info how to add your own format to a table. In this instance, the object has the following properties: samaccountname; homedirectory; extensionattribute2; extensionattribute3; mail; givenName; sn.
For each property, you will need to add a...
@{Expression={$_.samaccountname};Label="samaccountname";width=14}
So, you are taking the property ($_.samaccountname), naming the column in label and the width in... you guessed it... width. Anyway, full example s below.
$myformat = @{Expression={$_.samaccountname};Label="samaccountname";width=14},@{Expression={$_.homedirectory};Label="homedirectory";width=36},@{Expression={$_.extensionattribute2};Label="extensionattribute2";width=20},@{Expression={$_.extensionattribute3};Label="extensionattribute3";width=20},@{Expression={$_.mail};Label="mail";width=40},@{Expression={$_.givenName};Label="First Name";width=15},@{Expression={$_.sn};Label="Surname";width=15} $Result = $Result | Format-Table $myformat -Wrap | Out-String Write-host Write-host "Results" -ForegroundColor Green write-host $Result
And because why not, here is where the snippet of code was taken from.
$names = $null #$names = "BeetsonT","WrightS1","BerlinS","LeachK","peeta","WoodwardKD","NottR","CuskelJ","LatchfR","HunterC","LeggetP","ThomasC","SivellE","fraseraj","BougoureDP","SymeME","EdeAL","SmallcombeW" $names = Get-Content -Path C:\TEMP\user_list.txt $emails = @() $Result = @() Foreach($name in $names){ $checkem = $null write-host $name $checkem = get-aduser $name -properties * $e = $checkem.mail $myobjCheckem = "" | Select samaccountname, homedirectory, extensionattribute2, extensionattribute3, mail, givenName, sn $myobjCheckem.samaccountname = $checkem.samaccountname $myobjCheckem.homedirectory = $checkem.homedirectory $myobjCheckem.extensionattribute2 = $checkem.extensionattribute2 $myobjCheckem.extensionattribute3 = $checkem.extensionattribute3 $myobjCheckem.givenName = $checkem.givenName $myobjCheckem.sn = $checkem.sn $myobjCheckem.mail = $e $Result += $myobjCheckem $emails += "$e;" } $result | export-csv C:\TEMP\user_info.csv $myformat = @{Expression={$_.samaccountname};Label="samaccountname";width=14},@{Expression={$_.homedirectory};Label="homedirectory";width=36},@{Expression={$_.extensionattribute2};Label="extensionattribute2";width=20},@{Expression={$_.extensionattribute3};Label="extensionattribute3";width=20},@{Expression={$_.mail};Label="mail";width=40},@{Expression={$_.givenName};Label="First Name";width=15},@{Expression={$_.sn};Label="Surname";width=15} $Result = $Result | Format-Table $myformat -Wrap | Out-String Write-host Write-host "Results" -ForegroundColor Green write-host $Result Write-host "Emails" -ForegroundColor Yellow Write-host write-host $emails | Format-Table -AutoSize # clear clipboard and add the emails [Windows.Forms.Clipboard]::Clear(); [Windows.Forms.Clipboard]::SetText( $emails ) Write-host Write-host "Emails added to clipboard" -ForegroundColor DarkCyan