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

5

1

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

Posted 2016-03-06T23:42:28.443

Reputation: 639

Answers

4

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.

JosefZ

Posted 2016-03-06T23:42:28.443

Reputation: 9 121

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. – osullic – 2016-03-07T13:18:00.257

1For me Format-Table -AutoSize didn't help and strings were truncated anyway, whilst -width 300 did – Suncatcher – 2018-05-29T09:53:59.837

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

Falco Alexander

Posted 2016-03-06T23:42:28.443

Reputation: 896

That works, but each line gets enclosed in double quotes. So you'll have to deal with that if you don't want them. – Charles Burge – 2018-01-29T23:24:20.500

@CharlesBurge I think it depends on the culture and white space. But see my edit for a workaround. – Falco Alexander – 2018-02-01T10:13:37.787

@CharlesBurge To get ride of double quotes, just use -UseQuotes Never in the Export-CSV command. – frogcoder – 2019-08-20T04:30:22.237

1


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






joshua larsen

Posted 2016-03-06T23:42:28.443

Reputation: 11

0

When the returned values are strings you can pipe to out-string with width.

Get-ChildItem 'E:\' -Force -Recurse | Select-Object FullName | Out-String -Width 300 | 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.

Read more here Viewing Truncated PowerShell Output

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.

AndreasWBJ

Posted 2016-03-06T23:42:28.443

Reputation: 1

0

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.

roysubs

Posted 2016-03-06T23:42:28.443

Reputation: 1