Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

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

Writer Emily Wong

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?

8 Answers

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.

4

for me it works best with piping to export-csv

simply:

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

without double quotes:

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

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.

2

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.


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:\>

Following helped me:

$FormatEnumerationLimit=-1
Your Command | Format-Table | Out-File -width 999999 -Encoding utf8 "C:\Temp\result.txt"
2

I had this issue and it was due to errors in the output.

added -ErrorAction:silentlyContinue and output is displayed properly.

1

It will only work if the output is tableview. If the output is listview it's not going to work.

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

Read More Here: Fix Powershell Truncated Output

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy