Powershell – include Get-Date in .log file inside the Add-Content Cmdlet

I’ve made a small Powershell Script which deletes alle files and folders except specific ones.
The script itself works pretty good but I have a lot of troubble getting the logging to work. I’m currently on a good way with the Add-Content Cmdlet which works good. The only thing I now want to include is a small Get-Date Cmdlet inside the Add-Content which also includes the current time in the log when the specific file/folder was deleted. But I just can’t get it to work properly. Can someone help me?

Here is what I got so far:

Get-ChildItem -Path 'C:sample*notesdata' -Recurse -exclude names.nsf |
Select -ExpandProperty FullName |
Where {$_ -notlike 'C:sample*notesdataRoaming*'} |
Where {$_ -notlike 'C:sample*notesdataArchive*'} |
sort length -Descending |
Remove-Item -force -Recurse -Verbose 4>&1 | Add-Content -Path .ergebnis.log, .ergebnis2.log -Value (Get-Date)

The file “names.nsf” and the folders “Roaming”, “Archive” will not get deleted.

Thanks for your help 🙂

Go to Source
Author: Valle