Link to home
Start Free TrialLog in
Avatar of n0ch1ps
n0ch1psFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell - GCI output

Im playing with the following script to find files older than 1 day.

$DateToCompare = (get-date).AddDays(-1)
$logFiles = get-childitem C:\logs -recurse | ? {$_.LastWriteTime -lt $DateToCompare}
foreach ($f in $logfiles){
Write-Host $f.name
}

I can send the results via email, however, I need the name and lastwritetime of each file on a new line in the variable sent to the email function. In vb it would be something like:

foreach f in fld.files
fileformat = fileformat & vbcrlf
next
SendMail fileformat

Anyone know how this is done in PS? Cheers J
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Not sure why you would want to do this, but in powershell it would be like:

$DateToCompare = (get-date).AddDays(-1)
$logFiles = get-childitem C:\logs -recurse | ? {$_.LastWriteTime -lt $DateToCompare}
foreach ($f in $logfiles){
Write-output $f.name
Write-output $f.LastWriteTime
}

or

$DateToCompare = (get-date).AddDays(-1)
$logFiles = get-childitem C:\logs -recurse | ? {$_.LastWriteTime -lt $DateToCompare}
foreach ($f in $logfiles){
"{0}`n{1}" -f $f.name,$f.LastWriteTime
}
Avatar of n0ch1ps

ASKER

I've already said why, I need a variable to send to my mail sending function that formats the mail body properly I.E
ex2322323.log 28/09/2008 00:00:01
ex2322323.log 28/09/2008 00:00:01
ex2322323.log 28/09/2008 00:00:01
NOT
ex2322323.log 28/09/2008 00:00:01 ex23232323.log 28/09/2008 00:00:01 ex223322.log 28/09/2008 00:00:01

ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America 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
Avatar of n0ch1ps

ASKER

Thats the badger, thank you ;)