tags:

views:

634

answers:

1

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

+7  A: 

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

Joey
Perfect thanks. Btw, what's the "{0,-10}{1,-10}{2,-60}" doing in your example?
fenster
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: http://msdn.microsoft.com/en-us/library/txafckwd.aspx)
Joey