1
votes

When running the following line in PowerShell including the "Format-Table -AutoSize", an empty output file is generated:

Get-ChildItem -Recurse | select FullName,Length | Format-Table -AutoSize | Out-File filelist.txt

The reason I need the output file to be AutoSized is because longer filenames from the directoy are being trunacted. I am trying to pull all Filenames and File Sizes for all files within a folder and subfolders. When removing the -Autosize element, an output file is generated with truncated file names:

Get-ChildItem -Recurse | select FullName,Length | Out-File filelist.txt

3
why not just output to CSV using Export-Csv instead and the problem potentially goes away all together? You can even use a different delimiter than , if you don't like that. You basically can't do anything with a format-* output except look at it. But maybe that's all you need. - AdminOfThings

3 Answers

0
votes

Like AdminOfThings commented, use Export-CSV to get the untruncated values of your object.

Get-ChildItem -Recurse | select FullName,Length | Export-CSv -path $myPath -NoTypeInformation

I do not use Out-File much at all, and only use Format-Table/Format-List for interactive scripts. If I want to write data to a file, Select-Object Column1,Column2 | Sort-Object Column1| Export-CSV lets me select the properties of the object I am exporting that I want to export, and sort the records as needed. you can change the delimiter from a comma to tab/pipe/whatever else you may need.

0
votes

While the other answer may address the issue, you may have other reasons for wanting to use Out-File. Out-File has a "Width" parameter. If this is not set, PowerShell defaults to 80 characters - hence your issue. This should do the trick:

Get-ChildItem -Recurse | select FullName,Length | Out-File filelist.txt -Width 250 (or any other value)
0
votes

The Format-* commandlets in PowerShell are only intended to be used in the console. They do not actually produce output that can be piped to other commandlets.

The usual approach to get the data out is with Export-Csv. CSV files are easily imported into other scripts or spreadsheets.

If you really need to output a nicely formatted text file you can use .Net composite formatting with the -f (format) operator. This works similarly to printf() in C. Here is some sample code:

# Get the files for the report
$files = Get-ChildItem $baseDirectory -Recurse

# Path column width
$nameWidth = $files.FullName |
ForEach-Object { $_.Length } |
Measure-Object -Maximum |
Select-Object -ExpandProperty Maximum

# Size column width
$longestFileSize = $files |
ForEach-Object { $_.Length.tostring().Length } |
Measure-Object -Maximum |
Select-Object -ExpandProperty Maximum
# Have to consider that some directories will have no files with
# length strings longer than "Size (Bytes)"
$sizeWidth = [System.Math]::Max($longestFileSize, "Size (Bytes)".Length)

# Right-align paths, left-align file size
$formatString = "{0,-$nameWidth} {1,$sizeWidth}"

# Build the report and write it to a file
# ArrayList are much more efficient than using += with arrays
$lines = [System.Collections.ArrayList]::new($files.Length + 3)
# The [void] cast are just to prevent ArrayList.add() from cluttering the
# console with the returned indices
[void]$lines.Add($formatString -f ("Path", "Size (Bytes)"))
[void]$lines.Add($formatString -f ("----", "------------"))
foreach ($file in $files) {
    [void]$lines.Add($formatString -f ($file.FullName, $file.Length.ToString()))
}
$lines | Out-File "Report.txt"