# Format-Table adds a top empty line (row)

**URL:** <https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739>\
**Category:** PowerShell Help\
**Created:** [October 25, 2022, 4:29pm UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739 "2022-10-25T16:29:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joisrm](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Joisrm](https://forums.powershell.org/u/Joisrm)\
**Post date:** [October 25, 2022, 4:29pm UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/1 "2022-10-25T16:29:18Z")

</div>

I hope you all are doing well!  
I am exporting a directory file list to text with the following PS Code:

```auto
 Add-Type -AssemblyName System.Windows.Forms
  $saveDialog = New-Object -TypeName System.Windows.Forms.SaveFileDialog
  $PathFiles=(Get-ChildItem c:\temp\Files | Select-Object @{expression={$_.Name};label='Full Name'})
  $RegKeyContent = $PathFiles
  $saveDialog.initialDirectory = [environment]::getfolderpath(“mydocuments”)
  $saveDialog.filter = 'Text file (*.txt)|*.txt|All files (*.*)|*.*';
  $saveDialog.ShowDialog()
  $RegKeyContent | Format-Table * | Out-File $saveDialog.FileName

```

The code works as should but, when I open the text file there is a empty row on top as shown here:

![BlankSpace](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/5/50a245b7af0b6141cc96d742acc2f1e654f38eed.jpeg)

I have looked for but I have not been able to find how to remove the top empty line and, make sure the exported text starts on the very top.

Any suggestions is appreciated.  
Regards,

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 25, 2022, 6:50pm UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/2 "2022-10-25T18:50:56Z")

</div>

Joisrm,  
Welcome to the forum. 👋🏽

Format cmdlets like `Format-Table` or `Format-List` and so on are made to be used for formatting console output only and should always be the last cmdlet in a pipeline. If you pipe their output to the next cmdlet it’s in the vast majority of the cases wrong. 😉

Since you have only one column anyway you could use in this case `Out-File` directly. If you have a proper table you can use ‘Export-Csv’.  
So your last code line could be like this:

```auto
 $RegKeyContent | Out-File $saveDialog.FileName

```

Or like this:

```auto
 $RegKeyContent | Export-Csv -Path $saveDialog.FileName -NoTypeinfromation

```

In the latter case I’d recommend to cahnge the extension of the file to `CSV`. 😉

---

<div class="post-metadata">

**Author:** ![Joisrm](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Joisrm](https://forums.powershell.org/u/Joisrm)\
**Post date:** [October 25, 2022, 9:34pm UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/3 "2022-10-25T21:34:19Z")

</div>

Hi Olaf,  
I am running a PowerShell Runspace within my WinForms C# app. I am providing the user with exporting options such as; Text, HTML and CSV.

I already have all three-export options working. I thought there would be a way to remove the first blank space just to make it look nice.

Thanks.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 25, 2022, 11:45pm UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/4 "2022-10-25T23:45:24Z")

</div>

> [@Joisrm](#):
>
> I already have all three-export options working. I thought there would be a way to remove the first blank space just to make it look nice.

If you’re using `Format-Table` to pipe it to the next cmdlet you’re doing it wrong. 👆🏽 Have you tried my suggestion?

---

<div class="post-metadata">

**Author:** ![Joisrm](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Joisrm](https://forums.powershell.org/u/Joisrm)\
**Post date:** [October 26, 2022, 1:44am UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/5 "2022-10-26T01:44:45Z")

</div>

The only issue was the empty space on top that I wanted to remove when exporting to text file.

Using your code suggestion gives me the same empty space on top:

```auto
$RegKeyContent | Out-File $saveDialog.FileName

```

![Same Top empty space](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/a/a83e9cc9d11d32d5884342bf7993da6731be09e9.jpeg)

I already have a code to export to CSV that is working just fine. I do not need another Export-CSV code as you suggested:

```auto
 $RegKeyContent | Export-Csv -Path $saveDialog.FileName -NoTypeinfromation

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 26, 2022, 6:58am UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/6 "2022-10-26T06:58:28Z")

</div>

> [@Joisrm](#):
>
> Using your code suggestion gives me the same empty space on top:

I have to admit that I did not test my solution. I just assumed that it works. Sorry. 🙏🏼

Try it this way:

```auto
($RegKeyContent | Out-String).Trim() | 
    Out-File -FilePath $saveDialog.FileName

```

---

<div class="post-metadata">

**Author:** ![Joisrm](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Joisrm](https://forums.powershell.org/u/Joisrm)\
**Post date:** [October 26, 2022, 11:55am UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/7 "2022-10-26T11:55:21Z")

</div>

```auto
($RegKeyContent | Out-String).Trim() | 
    Out-File -FilePath $saveDialog.FileName

```

Your code worked!

![Now Working](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/e/ec6dac54a7706567e528bdb5f52bab4a3f147da1.jpeg)

Thank you

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/format-table-adds-a-top-empty-line-row/20739/8 "2024-05-16T20:16:26Z")

</div>


