2

So I would think that this would be a simple thing to do with powershell, but I can't seem to figure out how to make my array output as columns. Below is my script.

$Table = @()

$Employees = Get-ADUser -SearchBase "OU=Emplyoees,DC=mydomain,DC=com" -Filter * -Properties * | ?{$_.telephoneNumber -ne $Null}

ForEach($User in $Employees){$Table += @($User.Name,$user.telephoneNumber)}

($Table)

The output looks similar to:

User1
PhoneNumber1
User2
PhoneNumber2


What I would like it to look like is:

Name        PhoneNumber
User1        PhoneNumber1
User2        PhoneNumber2

Any help would be amazing

1 Answer 1

3

Try this:

ForEach($User in $Employees){$Table += ,@($User.Name,$user.telephoneNumber)} #NOTE THE COMMA HERE
$table | % { $_ -join '`t' } # the `t is a tab, but you can use whatever you want

Edit after comment:

if is just needed a layout you can try:

 $table | % {  "{0,-20}{1,20}" -f $_[0],$_[1]  } #the 2nd value in {} is the relative cursor position the sign left o right alignment

but why not use just $Employees variable to show the result:

$Employees = Get-ADUser -SearchBase "OU=Emplyoees,DC=mydomain,DC=com" -Filter * -Properties * | 
?{$_.telephoneNumber -ne $Null} | select name, telephonenumber | ft
Sign up to request clarification or add additional context in comments.

4 Comments

Kind of. I had to switch to doublequotes instead of singlequotes. Also the only issue is that since everyones name is a different length, the tabs don't make the telephone numbers lineup properly. Also before anyone asks, im outputting to a array because I will be adding a 3rd column once i get this to work.
@YuriOlinares I've added other solutions, you may need to adjust them based on future added columns
An array is a bad way to do this. Use the objects that PowerShell likes so much. Try C.B.'s last code with ft -autosize as the end. It will try to adjust the columns to show the full length of the values
Thank you for your input C.B, in the end i realized that i was trying to make this look pretty in a powershell window when the end result would be to copy and paste the results into an excel doc and then email it off to a manager. So as long as i can get all the info seperated by commas - will accomplish with a -join "," then i can pop it into excel no problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.