24

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 8

27

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
  • 1
    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
    Mar 7, 2016 at 13:18
  • 7
    For me Format-Table -AutoSize didn't help and strings were truncated anyway, whilst -width 300 did
    – Suncatcher
    May 29, 2018 at 9:53
  • 3
    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
    Sep 25, 2020 at 14:34
  • 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. Dec 7, 2020 at 11:57
7

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
4
  • That works, but each line gets enclosed in double quotes. So you'll have to deal with that if you don't want them. Jan 29, 2018 at 23:24
  • @CharlesBurge I think it depends on the culture and white space. But see my edit for a workaround. Feb 1, 2018 at 10:13
  • 1
    @CharlesBurge To get ride of double quotes, just use -UseQuotes Never in the Export-CSV command.
    – frogcoder
    Aug 20, 2019 at 4:30
  • 1
    This is the only solution that worked for me.
    – J Brune
    Mar 10 at 20:17
6

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
  • Doesnt Out-File support width too so you could forgo the Out-String?
    – Marie
    Feb 3, 2021 at 21:21
  • @Marie Sure, I have edited my answer so you can see the variations.
    – AndreasWBJ
    Mar 9, 2021 at 11:02
2

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






1

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.

1

Following helped me:

$FormatEnumerationLimit=-1

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

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

added -ErrorAction:silentlyContinue and output is displayed properly.

1
  • could you elaborate on this a little more?
    – Toto
    Apr 1 at 21:13
-2

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
  • 1
    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.
    – TylerH
    Sep 25, 2020 at 14:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.