Link to home
Start Free TrialLog in
Avatar of AXISHK
AXISHK

asked on

PowerShell to dump a particular folders

How to list all the folders and files with last modified date and send out an email using powershell ?

Tks
Avatar of SubSun
SubSun
Flag of India image

Try this code, this will report the details in an HTML mail.. You can also attach the details in a csv file if required.
$a = "<style>"
$a = $a + "BODY{background-color:White;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

$Body = Get-ChildItem D:\Folder -Recurse | 
   Select Name,FullName,LastWriteTime |
	 ConvertTo-HTML -head $a -body "<H3>Folder Information</H3>"

$Date = Get-date -format "dd/MM/yy HH:mm:ss"

Send-MailMessage `
	-Body ($Body | Out-String) `
	-Subject "Folder Information - $Date" `
	-From subsun@domain.com `
	-To subsun@domain.com `
	-SmtpServer smtp.domain.com

Open in new window

Avatar of AXISHK
AXISHK

ASKER

Tks. How to modify the code to include CSV as a attachment ?
Do you need both HTML & CSV reports? if yes try following code..
$a = "<style>"
$a = $a + "BODY{background-color:White;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
$Csv = "$($Env:TEMP)\Report.csv"

#collect data
$folder = Get-ChildItem D:\Folder -Recurse | Select Name,FullName,LastWriteTime
#Export to csv
$folder | Select Name,FullName,LastWriteTime | Export-Csv $Csv -nti
#Prepare HTML body
$Body = $folder | ConvertTo-HTML -Property Name,FullName,LastWriteTime -head $a -body "<H3>Folder Information</H3>"

$Date = Get-date -format "dd/MM/yy HH:mm:ss"

Send-MailMessage `
	-Body ($Body | Out-String) `
	-Subject "Folder Information - $Date" `
	-From subsun@domain.com `
	-To subsun@domain.com `
	-SmtpServer smtp.domain.com `
	-Attachments $Csv

Open in new window

Avatar of AXISHK

ASKER

Send-MailMessage `
      -Body ($Body | Out-String) `
      -Subject "Folder Information - $Date" `
      -From subsun@domain.com `
      -To subsun@domain.com `
      -SmtpServer smtp.domain.com `
      -Attachments $Csv

What's the meaning for '  at the end of each statement ?
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial