Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output data with no column headings using PowerShell

Tags:

powershell

I want to be able to output data from PowerShell without any column headings. I know I can hide the column heading using Format-Table -HideTableHeaders, but that leaves a blank line at the top.

Here is my example:

get-qadgroupmember 'Domain Admins' | Select Name | ft -hide | out-file Admins.txt 

How do I eliminate the column heading and the blank line?

I could add another line and do this:

Get-Content Admins.txt | Where {$_ -ne ""} | out-file Admins1.txt 

But can I do this on one line?

like image 727
fenster Avatar asked Sep 10 '09 22:09

fenster


People also ask

How do I display output in a Table Format in PowerShell?

The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column. You can use the Property parameter to select the properties that you want to display.

How do I use AutoSize in PowerShell?

If you specify the AutoSize parameter when you run the Format-Table command, PowerShell calculates column widths based on the actual data displayed. This makes the columns readable. The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.

What is FT in PowerShell?

Windows PowerShell Scripting – Format-Table (FT) Format-Table, or FT for short, controls the formatting of the output of your Windows PowerShell commands. Whenever presentation of information is important, pipe the script's output into Format-Table.


2 Answers

In your case, when you just select a single property, the easiest way is probably to bypass any formatting altogether:

get-qadgroupmember 'Domain Admins' | foreach { $_.Name } 

This will get you a simple string[] without column headings or empty lines. The Format-* cmdlets are mainly for human consumption and thus their output is not designed to be easily machine-readable or -parseable.

For multiple properties I'd probably go with the -f format operator. Something along the lines of

alias | %{ "{0,-10}{1,-10}{2,-60}" -f $_.COmmandType,$_.Name,$_.Definition } 

which isn't pretty but gives you easy and complete control over the output formatting. And no empty lines :-)

like image 185
Joey Avatar answered Sep 20 '22 13:09

Joey


A better answer is to leave your script as it was. When doing the Select name, follow it by -ExpandProperty Name like so:

Get-ADGroupMember 'Domain Admins' | Select Name -ExpandProperty Name | out-file Admins.txt 
like image 36
GavinBurke Avatar answered Sep 20 '22 13:09

GavinBurke