Output data with no column headings powershell

Tag: powershell Author: sansheng6078 Date: 2009-08-25

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 I would like to do this on one line if possible

Best Answer

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 :-)


Perfect thanks. Btw, what's the "{0,-10}{1,-10}{2,-60}" doing in your example?
That's a format string. Usually format strings consist of something in braces. First a numeric index which refers to which item of the object array to format is to be substituted there, you can give a width after the comma. Negative forces left alignment instead of right alignment. Above example will output the command type, the name and the definition of an alias in columns of 10, 10 and 60 characters respectively. It all makes sense once you understand how format strings work :-) (reference: msdn.microsoft.com/en-us/library/txafckwd.aspx)

Other Answer1

If you use "format-table" you can use -hidetableheaders

Other Answer2

Joey mentioned that Format-* is for human consumption. If you're writing to a file for machine consumption, maybe you want to use Export-*? Some good ones are

  • Export-Csv - Comma separated value. Great for when you know what the columns are going to be
  • Export-Clixml - You can export whole objects and collections. This is great for serialization.

If you want to read back in, you can use Import-Csv and Import-Clixml. I find that I like this better than inventing my own data formats (also it's pretty easy to whip up an Import-Ini if that's your preference).