Free PowerShell Tutorial – How To Create A New File Each Day With PowerShell

Ad

If you need to automate a task to create a file once a day, this PowerShell tutorial will help.

This problem is handled in 2 parts:  Doing the task you want to do; and saving the output.

First things first: We need something to do

Since this post is really about getting an automated process running, this doesn’t have to be fancy.

Ad

Let’s perform a really simple command (get-service) and send it through the pipe so we can filter out anything that isn’t running, saving it as a variable called $svcs.

$svcs = get-service | ? {$_.status –eq “Running”}

Secondly, we format and save the output

Since the idea is to create a new file each day, we’ll work on the filename to make sure that it’s right.

$filepath = "c:\scripts\output\dailyservicereports\" + (date -f yyyy-MM-dd) + "-DSR.html"

Working with the date formatting can be tricky – it’s case-sensitive and you will have to remember that it’s lowercase for the years and days, but uppercase for the months.  Also, the hyphens (like yyyy“-”MM) works just fine because the “-“ isn’t confused for one of the date time properties (minute, hour, etc) so it’s included as part of the formatting.  Any character that isn’t (“m for minute” or “M for Month”) is included in the formatting. 

Ad
Find Tech Jobs on Dice!

$svcs | select servicename, displayname | sort servicename | convertto-html | out-file $filepath

That should just about do it.  Saved in your output directory is a simple webpage you can view.  Drop it instead into your C:\wwwroot and connect up to it via IIS if you want to be extra tricky.

Now that you’re ready to create a new file each day – you’d better learn how to run the script as a scheduled task.

Ad
Find Tech Jobs on Dice!
CategoriesUncategorized