Powershell 'for each' loop output without heading

Viewed 43

i am having a problem with the output of for-each loop. i am getting the desired output but for the parameters that I have selected, the heading appears before each line which i don't want.

here's my code:

$list = Get-Content C:\Temp\Description\Servers.txt
Foreach ($server in $list)
{
Get-ADComputer -identity $server -properties * | select name,description | ft -autosize
}

please see image below for current and desired output

Output I am getting & the one I want.

1 Answers

As Santiago Squarzon commented, you don't need Format-Table, unless there are more than three columns. In this case PowerShell would default to list format and you'd have to force it to table format using Format-Table.

So this should work in your case:

$list = Get-Content C:\Temp\Description\Servers.txt
Foreach ($server in $list)
{
    Get-ADComputer -identity $server -properties * | select name,description
}

In cases where you actually have to use Format-Table to force the desired output, make sure that it is the last command of the pipeline:

Get-Content C:\Temp\Description\Servers.txt | ForEach-Object {
    Get-ADComputer -identity $_ -properties * | select name,description
} | Format-Table -autosize
Related