Excessive Width From Get-ChildItem or Out-String or Send-MailMessage

I am not sure which of the cmdlets is producing the excessive width or if any are … perhaps something else is at work here.

I currently am using Get-ChildItem -Path L:\ | Out-String assigned to a variable to capture the contents of a directory.

That variable is used with -Body from the Send-MailMessage cmdlet

But when the output arrives in the e-mail the output is very wide and the columns like MODE, LastWriteTime, and such scrunched together.


Mode LastWriteTime Length Name

---- ------------- ------ ---- (these don’t line up under the heading)

-a— 10/30/2020 12:31AM RM.0.FTF.DBPART100.2020102513090000009 (then lots of space this way ->)
76 (and some lines wrap and have a number which I don’t know what it is)

-a— 10/30/2020 12:32 AM TM.0.FIF.DBPART100.2020102513110000009 (then lots of space this way ->)
(and some lines wrap and don’t have this extra number)


With Out-String I have tried a few parameters like -Width 80 but that does something odd. Using the first example line from above (the RM) file becomes:

-a— 10/30/2020 12:31AM RM.0.FTF.DBPART100.20201025130900000… (then lots of space this way ->)
76 09 (why does the 09 appear on the right instead of left like 09 76)
(if I arrow-right to the end of the line, the cursor wraps normally and proceeds to the 76 on the next line)


Anyway … Can this be formatted better? Or can something produce the output you get with the old dir.exe

Kind regards to all

When formatting for an email, leverage HTML. There is an ebook in the Free Resources that talks about reporting in HTML. The general idea:

$files = Get-ChildItem -Path C:\Scripts\*.txt | Select-Object -Property LastWriteTime, FullName

$mailParams = @{
    To         = 'user@myemail.com'
    From       = 'Pretty Name <do_not_reply@myemail.com>'
    Subject    = 'Stuff from Get-ChildItem'
    Body       = ($files | ConvertTo-Html -Title 'My Files' | Out-String)
    BodyAsHtml = $true
    SmtpServer = 'smtp.myemail.com'

Send-MailMessage @mailParams

Thanks Rob. I will give that a try shortly. Just wanted to report, I figured out what the extra number was. Because of the scrunching the 76 is actually part of the Length i.e. file size which I unfortunately did not include in my examples above. The file size is: 121,396,043,776

Thanks Rob, this worked nicely. I will use that source you referenced to see if it has info on making the output sorted by date.

$files = Get-ChildItem -Path C:\Scripts\*.txt | 
         Sort-Object -Property LastWriteTime -Descending |
         Select-Object -Property LastWriteTime, FullName

Ok. Show off. :slight_smile: LOL. But, really, thank you.