Output data with no column headings using PowerShell

Viewed 261144

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?

8 Answers

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

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}
$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

Related