61

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fenster
  • 1,809
  • 6
  • 22
  • 24

8 Answers8

67

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
  • 344,408
  • 85
  • 689
  • 683
  • Perfect thanks. Btw, what's the "{0,-10}{1,-10}{2,-60}" doing in your example? – fenster Sep 11 '09 at 15:02
  • 6
    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 Sep 11 '09 at 17:44
  • @Joey, your answer answered so many questions for me... Thanks a lot – Jesus Uzcanga Mar 30 '21 at 15:29
55

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
P-L
  • 523
  • 4
  • 14
GavinBurke
  • 665
  • 5
  • 7
42

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

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
egon
  • 429
  • 4
  • 2
  • 3
    Please note the problem is NOT about hiding the headers, but about the blank lines produced WHEN you're already hiding the headers with `-hidetableheaders` – curropar Feb 19 '19 at 13:37
  • 2
    Please note this will hide the headers as requested, AND remove any blank lines: (get-qadgroupmember 'Domain Admins' | Select Name | ft -hidetableheaders | Out-String).trim() – ScriptMonkey Aug 21 '19 at 03:02
  • Thanks @ScriptMonkey! Your solution with `.trim()` in the comments resulted in my **$winProductName** string returning `Windows 10 Home` which is exactly what I wanted. – Eric Hepperle - CodeSlayer2010 Jan 13 '23 at 14:40
10

add the parameter -expandproperty after the select-object, it will return only data without header.

Dominic Caron
  • 111
  • 1
  • 4
9

The -expandproperty does not work with more than 1 object. You can use this one :

Select-Object Name | ForEach-Object {$_.Name}

If there is more than one value then :

Select-Object Name, Country | ForEach-Object {$_.Name + " " + $Country}
Lyric Tropsed
  • 111
  • 1
  • 4
3

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

Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
2

First we grab the command output, then wrap it and select one of its properties. There is only one and its "Name" which is what we want. So we select the groups property with ".name" then output it.

to text file

 (Get-ADGroupMember 'Domain Admins' |Select name).name | out-file Admins1.txt

to csv

(Get-ADGroupMember 'Domain Admins' |Select name).name | export-csv -notypeinformation "Admins1.csv"
maazza
  • 7,016
  • 15
  • 63
  • 96
Powerboy2
  • 69
  • 5
-1
$server = ('*')+(Read-Host -prompt "What Server Context?")+'*'
$Report = (Get-adcomputer -SearchBase "OU=serverou,DC=domain,DC=com" -filter {name -like $server} -SearchScope Subtree|select Name |Sort -Unique Name) 
$report.Name | Out-File .\output\out.txt -Encoding ascii -Force
$Report
start notepad .\output\out.txt

Put your server SearchBase in above. If you are not sure what your server OU is try this function below...

#Function Get-OSCComputerOU($Computername)

{
    $Filter = "(&(objectCategory=Computer)(Name=$ComputerName))"

    $DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher
    $DirectorySearcher.Filter = $Filter
    $SearcherPath = $DirectorySearcher.FindOne()
    $DistinguishedName = $SearcherPath.GetDirectoryEntry().DistinguishedName

    $OUName = ($DistinguishedName.Split(","))[1]
    $OUMainName = $OUName.SubString($OUName.IndexOf("=")+1)
    
#    $Obj = New-Object -TypeName PSObject -Property @{"ComputerName" = $ComputerName
#                                                     "BelongsToOU" = $OUMainName
#                                                     "Full" = $DistinguishedName}
    $Obj = New-Object -TypeName PSObject -Property @{"Full" = $DistinguishedName}


    $Obj
}

Makes sure to run the Get-OSCComputerOU Servername with a select -expandproperty Full filter. Then just plug in the response to the Searchbase...

All thanks to http://www.jaapbrasser.com

Patrick Burwell
  • 129
  • 1
  • 12