Out-File2: Improving Out-File with automatic file backup

Have you ever needed to write some file, but the need for making sure the file size didn’t get out of hand made you have to write a ton of extra code? This is a very typical scenario when writing log files for instance. Out-File2 got you covered!

This function is a wrapper around Out-File, but adds the ability to automatically create backups of the file you are writing to if the file size is larger than what you specify. More over, you can specify how many backups you want to keep, and it will clean up the oldest files when that threshold is reached. You can choose between two different file suffixes; a counter and a date stamp.

You can customize the suffix separator, as well as the number of digits for the counter and the date format for the date suffix. Note that the default value for MaxFileSize is 0, which makes the function behave just like the regular Out-File cmdlet. If you want the advanced functionality you should set MaxFileSize to some value you are comfortable with, as well as use the Append switch.

This function is doing a lot of stuff, so please report any bugs to me (or fork the Gist and help me do it).

Oh! And if you come up with a better name than Out-File2, please let me know 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s