How to fix truncated PowerShell output, even when I've specified -width 300

90,002

Solution 1

Pipe output to Format-Table commandlet, e.g. as follows:

Get-ChildItem 'E:\' -Force -Recurse | Select-Object FullName | Format-Table -AutoSize

or

(Get-ChildItem 'E:\' -Force -Recurse).FullName | Format-Table -AutoSize

Note that -Width parameter of Out-File cmdlet specifies the number of characters in each line of output. Any additional characters are truncated, not wrapped. However, -Width 300 should suffice in this case.

Solution 2

for me it works best with piping to export-csv

https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Export-Csv?view=powershell-3.0

simply:

GCI 'E:\' -Recurse | Select FullName | Export-CSV Files.csv

without double quotes:

Add-Content -Path Files.txt -Value (GCI 'E:\' -Recurse).FullName

Solution 3

First you need to make sure Select-Object is not truncated, by using ExpandProperty for a single property

Get-ChildItem 'E:\' -Force -Recurse | Select-Object -ExpandProperty FullName | Out-File -Encoding utf8 "C:\Users\Me\Desktop\listing.txt"

Or by pipe to format-table when there are more properties.
Format-table will select values to display if there is not space enough in console, even with parameters autosize and wrap.
So you will need to pipe to Out-String with width parameter to see it all in console or add width to Out-File to see it all in output file.

Get-ChildItem 'E:\' -Force -Recurse | Select-Object * | Format-Table | Out-String -width 9999

and (

Get-ChildItem 'E:\' -Force -Recurse | Select-Object * | Format-Table | Out-File -width 9999 -Encoding utf8 "C:\Users\Me\Desktop\listing.txt"

or

Get-ChildItem 'E:\' -Force -Recurse | Select-Object * | Format-Table | Out-String -width 9999 | Out-File -Encoding utf8 "C:\Users\Me\Desktop\listing.txt"

)
When values are in a collection/array the output is controlled by $FormatEnumerationLimit
Setting it to -1 means unlimited.

I know this is an old post, but I found it before the answer I was looking for.
So thought I would share my findings.

Solution 4

I would use ConvertTo-Csv (just the same as Export-Csv but does not send to file) as you can easily continue to manipulate the results as required.

gci 'C:\Program Files\'

gci 'C:\Program Files\' | Select Mode,LastWriteTime,Length,Name | ConvertTo-Csv

This now guarantees that you have strings (not objects!) and that none of the data has been truncated, so you can continue to use any other string manipulations that you want:

gci 'C:\Program Files\' | Select Name | ConvertTo-Csv | Select-String "Win"

(gci 'C:\Program Files\' | Select Name | ConvertTo-Csv) -Replace """", ""

Second one gets rid of the " characters added by CSV encoding.

Notes:

The <verb>-Csv Cmdlets will output all Property types (AliasProperty, NoteProperty, Property, ScriptProperty, etc). You can easily check that with:

gci 'C:\Program Files\' | Get-Member *Property

Also recommend using the -No (-NoTypeInformation) switch to get rid of the #TYPE Selected.System.IO.DirectoryInfo that is at the top of all outputs.

You always guarantee that you are working with non-truncated values with this method. With this I can usually organise the information that I need before throwing out to a file or screen.

Solution 5


You need to mash the answers from andreaswbj & josefz together to do what I think you are trying to do.


PS:\> $FormatEnumerationLimit = -1   
PS:\> $Console = $Host.UI.RawUI 
PS:\> $Buffer = $Console.BufferSize  
PS:\> $Buffer.Width = '4096'
PS:\> $Console.BufferSize = $Buffer 
PS:\> $Var = [PsCustomObject]@{Stdout = $("*"*1024)} 
PS:\> $Var

Stdout
------
****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************


PS:\>






Share:
90,002

Related videos on Youtube

osullic
Author by

osullic

Updated on September 18, 2022

Comments

  • osullic
    osullic over 1 year

    I'm trying to get a list of all the directories and files on an external hard drive, with one fully-qualified directory/file path per line. I'm using PowerShell on Windows 10 to achieve this. This is the PowerShell command I'm using:

    Get-ChildItem 'E:\' -Force -Recurse | Select-Object FullName | Out-File -Encoding utf8 "C:\Users\Me\Desktop\listing.txt" -width 300
    

    However, some paths are being truncated in my output. In fact, over 10,000 lines are truncated. Here is an example of one line in my listing.txt PowerShell output file:

    E:\Sort\Tech Stuff\2006-2007 CompanyLtd\fsie01-Engineering-Users-User1\Reference\LCID & Language Group by Language\Configuring and Using International Features of Windows Windows 2000 - List of Locale I...
    

    When I browse to this directory in File Explorer (E:\Sort\Tech Stuff\2006-2007 CompanyLtd\fsie01-Engineering-Users-User1\Reference\LCID & Language Group by Language), the file there is called 'Configuring and Using International Features of Windows Windows 2000 - List of Locale IDs and Language Groups.url'. The path of this file is 228 characters long if I haven't miscounted, which should be within acceptable limits.

    What am I doing wrong that is truncating the paths in my PowerShell output?

  • osullic
    osullic about 8 years
    Thanks - that seems to allow me to get the output I want. However, there's still no explanation as to what was wrong with my initial command, which looks like it should do what I want. Any idea? It's not intuitive that paths would be (apparently arbitrarily) truncated at ~205 characters unless one uses a format-table command.
  • Charles Burge
    Charles Burge over 6 years
    That works, but each line gets enclosed in double quotes. So you'll have to deal with that if you don't want them.
  • Falco Alexander
    Falco Alexander over 6 years
    @CharlesBurge I think it depends on the culture and white space. But see my edit for a workaround.
  • Suncatcher
    Suncatcher almost 6 years
    For me Format-Table -AutoSize didn't help and strings were truncated anyway, whilst -width 300 did
  • frogcoder
    frogcoder over 4 years
    @CharlesBurge To get ride of double quotes, just use -UseQuotes Never in the Export-CSV command.
  • TylerH
    TylerH over 3 years
    I had to use Format-Table -Wrap -Autosize for my case; was still getting truncated column values in multiple columns of output even after using Format-Table -Autosize
  • TylerH
    TylerH over 3 years
    This appears to just be the exact same solution as the accepted solution (from 4 years ago). Please be sure to read existing answers before posting a new one to ensure you avoid duplicate of content.
  • CvRChameleon
    CvRChameleon over 3 years
    I can confirm that -Autosize on its own does nothing much different in my case either, Format-Table -Wrap -Autosize is probably going the be the better solution.
  • Marie
    Marie over 3 years
    Doesnt Out-File support width too so you could forgo the Out-String?
  • AndreasWBJ
    AndreasWBJ about 3 years
    @Marie Sure, I have edited my answer so you can see the variations.
  • DarkDiamond
    DarkDiamond about 2 years
    Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review
  • Saaransh Garg
    Saaransh Garg about 2 years
    @DarkDiamond I don't think it's a "thank you" answer. He posted a solution of his own.
  • J Brune
    J Brune about 2 years
    This is the only solution that worked for me.
  • Toto
    Toto about 2 years
    could you elaborate on this a little more?