powershellcharacter-limit

List all files with size using powershell char limit issue


I have an applications folder that have more than 10 applications in. I want to list all files (including sub-folders) with directory and size info and save it under each application folder.

Here is the script (it is in C:\Users\xxxxxx\Desktop\apps)

$FolderList = Get-ChildItem -Directory
foreach ($folder in $FolderList)
{
    $thisPath = Get-Location
    Get-ChildItem -File -Filter * -Path $folder -Recurse |
    Sort-Object -Property Length -Descending|
    Select-Object -Property FullName, Length, @{l='Length (MB)';e={$_.Length/1MB}} | 
    Format-Table -AutoSize | 
    Out-File $thisPath\fileList_$folder.txt
}

Output:

FullName - Length - Length (MB)


C:\Users\xxxxxx\Desktop\apps\3\VSCodeUserSetup-x64-1.62.2.exe 79944464 76.2409820556641 C:\Users\xxxxxx\Desktop\apps\3\son.zip 18745870 17.8774547576904

It does what I want but in some of the outputs where the path is too long it didn't write the length or even full path.

FullName


C:\Users\xxxxxx\Desktop\apps\3\xxxxxxxxxxx/xxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxx/VSCodeUserSetu...
C:\Users\xxxxxx\Desktop\apps\3\son.zip

As I searched I saw that there is a char limit. How can I overcome this issue?


Solution

  • The -Path parameter is defined as a string array, but you are feeding it a DirectoryInfo object.

    The second part of your question is about truncation, which happens because you use Format-Table -AutoSize on the data.

    Format-* cmdlets are designed to display output on the console window only which has a limited width and all items longer are truncated.
    Rule of Thumb: never use Format-* cmdlets when you need to process the data later.

    Instead of saving a formatted table to a text file, I would suggest saving the (object) info as structured CSV file which makes working with the data later MUCH easier and without truncation. (you can for instance open it in Excel)

    # just get an array of Full pathnames
    $FolderList = (Get-ChildItem -Directory).Name #instead of Fullname
    foreach ($folder in $FolderList) {
        # create the path for the output
        $fileOut = Join-Path -Path $folder -ChildPath ('filelist_{0}.csv' -f $folder)
        Get-ChildItem -File -Path $folder -Recurse |
        Sort-Object -Property Length -Descending |
        Select-Object -Property FullName, Length, @{l='Length (MB)';e={$_.Length/1MB}} | 
        Export-Csv -Path $fileOut -NoTypeInformation -UseCulture
    }
    

    I added switch -UseCulture to the Export-Csv cmdlet so you can afterwards simply double-click the csv file to open it in your Excel