'User PowerShell to Delete Contents of Folder, but Ignore Files Currently in Use

Long story short, I have a customer who is trying to figure out why logs are filling up their server. That's not really something I need to worry about, other than I need to continually go through and clear out logs from a ton of servers so my application can run. I'm trying to figure out a script that will clear out the log folder. Ideally, this should work:

Get-ChildItem -Path $filePath -Include *.* -Recurse | foreach { $_.Delete()}

but some of the files are still in use, so I'm getting this:

Exception calling "Delete" with "0" argument(s): "The process cannot access the file 
'filename' because it is being used by another process."
At line:1 char:87
+ ... $filePath -Include *.* -Recurse | foreach { $_.Delete()}
+                                                           
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IOException

Is there something I can add to this to ignore files that are still in use?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source