Truncate log files with PowerShell
It is a very common task to truncate log files. You don't notice them and they reach the limit size always not in the appropriate time. And when this moment occurs, it's much better to have a quick solution to decrease the size of log files. But even much better to clean logs automatically on the schedule.
Some time ago I wrote about how to truncate log files in Linux operational systems. Now it's time to write about Windows.
In Windows there is a PowerShell cmdlet which you can use to clear contents of a file.
To clear contents of a single file:
Clear-Content C:\Temp\TestFile.txt
To clear contents of all files with the ".log" file extension:
Clear-Content -path * -filter *.log –force
"Clear-Content" only clears the contents of a file. It doesn’t delete the file. To delete the file use cmdlet Remove-Item:
Remove-Item C:\temp\file2delete.txt