0

UPDATE: PSVersion 5.1.19041.610

I'm trying to do what I thought was quite simple: save a file list (including some meta-info) in a text file.

Get-ChildItem | Out-File test.txt

This should do the trick, right?

Unfortunately, every filename that's too long gets wrapped, like following.

Directory: C:\temp\test_file_names


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        03.02.2021     13:16              8 some_very_very_very_very_very_very_very_long_f
                                                  ile_name.file

Using the -Width parameter of Out-File removes the wrapping.

Get-ChildItem | Out-File -Width 300 test.txt

But now every line is padded with spaces to be exactly 300 wide.

Changing $Host.UI.RawUI.BufferSize has the same effect.

Is it possible to somehow get no wrapping and no padding?

Tymur Gubayev
  • 468
  • 4
  • 14

1 Answers1

1

This seems to be an issue of PS 5 only. Can't reproduce in PS 7.1.

Workaround for PS 5:

Get-ChildItem | Out-String -Width 300 -Stream | %{ $_.TrimEnd() } | Out-File test.txt

Parameter -Stream is used so we process each line separately.

zett42
  • 25,437
  • 3
  • 35
  • 72
  • thanks, that's what I needed. Guess I'll need to upgrade to avoid this kind of headache. – Tymur Gubayev Feb 03 '21 at 13:26
  • PowerShell v5x and earlier is completely separate environments from PowerShell Core (v6+). There is no upgrade from Powershell 5x to Powershell Core. It's an install PowerShell Core, thus running PowerShell 5x and PowerShell v6+side-by-side. Unless your whole enterprise/organization is PSCore, then you need to deal with whatever PS version(s) are in your environment. – postanote Feb 03 '21 at 18:19