Enhance logging format for powershell output

Hey Experts!  As I evolve my script, I wanted to improve the log file format.  I need to help with the programming portion.  

Right now the script will delete the files that meet the criteria set and output basic info to a log file.

$logfile = "C:\Tools\Logs\SW_Cleanup.log"
cd "C:\tools\Scripts\Test"

forfiles /d -0 /c "cmd /c echo @path @fdate" > $logfile
forfiles /d -0 /c "cmd /c del /q /f @file"

Open in new window

I would like to include a header like this:

$scriptversion = 10.12.18

Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]."
Add-Content -Path $sFullPath -Value "***************************************************************************************************"
Add-Content -Path $sFullPath -Value ""
Add-Content -Path $sFullPath -Value "SpiceWorks purge of backups older than 10 days"
Add-Content -Path $sFullPath -Value ""
Add-Content -Path $sFullPath -Value "Running script version [$ScriptVersion]."
Add-Content -Path $sFullPath -Value ""
Add-Content -Path $sFullPath -Value "***************************************************************************************************"
Add-Content -Path $sFullPath -Value "

Through my testing, I've tried various add-content and write-log options with no success so I figured it was time to ask the professionals.  I appreciate your time and assistance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

samiam41Author Commented:
$StartFolder = "C:\tools\Scripts\Test"
$logfile = "C:\Tools\Logs\SW_Cleanup.log"
$version = "10.12.18"

Purge of SpiceWorks backups older than 10 days

Ran on: $(Get-Date -format 'yyyy-MM-dd HH:mm:ss')

Running script version $version

"@ | Out-File $logfile


dir $StartFolder -Recurse -Force -ea 0 | 
?{!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-0)} | 
ForEach-Object {
	$_ | del -Force
	$_.FullName | Out-File C:\Tools\Logs\SW_Cleanup.log -Append


Open in new window

I used -0 days so that I could test out the script and delete out any files 0 days old (essentially all).  I'm changing it back to -10 so it will only delete those files that are older than that.
samiam41Author Commented:
See my post above.

Thanks anyways Experts!  Catch up with you next time.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPRetiredCommented:
$Scriptversion = '10.12.18'
$logfile = "C:\Tools\Logs\SW_Cleanup.log"
$path = 'C:\tools\Scripts\Test'
$now = get-date -Format "dd/MMM/yy hh:mm"
$log = @()
$log += "Started processing at [$([DateTime]::Now)]."
$log +=  "***************************************************************************************************"
$log += " "
$log +=  "SpiceWorks purge of backups older than 10 days"
$log += "Running script version [$ScriptVersion]."
$log += " "
$log += "***************************************************************************************************"
$log += " "
$deletedfiles = 0
$files = get-childitem -Path $path
$10days = (get-date).AddDays(-10)
#$10days = (get-date).AddDays(-0)
foreach ($file in $files){
  if ($file.LastWriteTime -le $10days){
    remove-item -Path $file.fullname -Force
    $log += "Deleted:" + $file.name
    $deletedfiles ++

$log += "Deleted: " + $deletedfiles + " Files"
$log | out-file $logfile

Open in new window

samiam41Author Commented:
Actually going to take parts out of your script and incorporate into mine so I thought it only fair to include you in the answer process.  Thank you for your time and script!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.