Link to home
Create AccountLog in
Avatar of jimmylew52
jimmylew52Flag for United States of America

asked on

Powershell script to start new line after each file name in email.

The following script works well but I need a new line to start after each file name.

$fileCount = (Get-ChildItem E:\Test2 | ? {! $_.PsIsContainer} | Measure).count
  If ($fileCount -gt 0)
  {Send-MailMessage -From 'files@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found' `
  -Body  $("The Following Files Were found On `n server 16 Test2 `n" +`
     ((Get-ChildItem "E:\Test2"`
     | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n"))`
}
Avatar of Qlemo
Qlemo
Flag of Germany image

You are collecting the same info twice (with Get-ChildItem) - this can be optimized.
The newline is there - but sometimes we need a carriage return instead, so try with `r:
$files = Get-ChildItem E:\Test2 | ? {! $_.PsIsContainer}
if ($files.Count -gt 0)
{
  Send-MailMessage -From 'files@mycompany.com' -To 'me@mycompany.com' `
    -SmtpServer 'smtp.mycompany.com' `
    -Subject 'Files Found' `
    -Body  @"
The Following Files Were found On 
server 16 Test2 
$(($files | ft -auto name, CreationTime | out-string) -join "`r")
"@
}

Open in new window

Avatar of jimmylew52

ASKER

Thank you for your input.

If I replace the n with r I do not get the files listed on separate lines.

When I run you script the files list the same as my script, not on different lines.
Is there any way to put a new line between     ft -auto name and CreationTime? That may get me the results I need. U need ti see the time stamp for the files when the files are really long. Sometimes they are long enough the time stamp is left off.
Your eMail client is set up to remove single carriage returns/line feeds then for plain text. In that case we would need to join the lines with either "`r`r" or "`n`n".
I recommend you use HTML tables instead:
$files = Get-ChildItem E:\Test2 | ? {! $_.PsIsContainer}
if ($files.Count -gt 0)
{
  Send-MailMessage -From 'files@mycompany.com' -To 'me@mycompany.com' `
    -SmtpServer 'smtp.mycompany.com' `
    -Subject 'Files Found' `
		-BodyAsHtml `
    -Body  @"
The Following Files Were found On 
server 16 Test2 
$(($files | select name, CreationTime | ConvertTo-HTML) -join "`n")
"@
}

Open in new window

I tried your script with the html and `n`n and/or `r`r and/or `r`n. I still do not get a line break between the file names. Here is what I get:

Name                    CreationTime        
----                    ------------        
New Text Document3A.txt 2/18/2013 2:26:40 PM New Text Document3B.txt 2/18/2013 2:26:40 PM New Text Document3C.txt 2/18/2013 2:28:09 PM

Also, with your script I do not get an emali unless there is more than 2 files in the folder E:\Test2.
I had an error in your script. The files get listed on separate lines but the script does not send an email if there is only one file in the folder.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thank You. That fixed that problem.

Problem with the length of the file names went away using the HTML.