I often make use of temporary files in my PowerShell scripts to collect data for further analysis. Hard-coding the temporary file names is always a bad idea because you might unintentionally overwrite a file or create conflicts with scripts that are running concurrently.

Generate a Unique File Name

To create a unique file name, I chose to use the get-date cmdlet to create a name consisting of the year, month, day, hour, minute, second, and millisecond components. It’s not as unique as a GUID but for my purposes, this will work fine with very little chance of two scripts generating the same name. Also, the temporary files will be listed in a chronological order and be in a human-readable format if I ever have to go back and look at one for debugging purposes.

To create the file, there are a couple of options:

Option 1: Use Get-Date with the -format option :

?View Code POWERSHELL
$temp_file =  "$(Get-Date -format 'yyyy_MM_dd_hh_mm_ss').csv"

The problem here is that “-format” does not provide an option for including milliseconds. If you don’t need to worry about two scripts running this line of code within the same second, then this is fine. However, I prefer to go the extra step and avoid that possibility.

Option 2: Save date to a variable and then format in a second step (more code, but better) :

?View Code POWERSHELL
$x = Get-Date
$temp_file = [string]::format("tempfile_{0}_{1:d2}_{2:d2}_{3:d2}_{4:d2}_{5:d2}_{6:d4}.csv",
  $x.year,$x.month,$x.day,$x.hour,$x.minute,$x.second,$x.millisecond)

So now that we have a unique temporary filename, where are we going to put it? What directory will it go in? I personally like to avoid cluttering up my scripts directory so I definitely want to place it elsewhere. One option is to use the $env:temp location but I choose to create a separate temp directory branching off from my current working directory. I’ll call this directory “temp” and all my temporary work files will be placed here until I choose to delete them.

Create a Temp Directory (if needed)

Here’s the code to create the temp directory if it’s needed:

?View Code POWERSHELL
if (!(test-path temp))
 {new-item temp -type directory}

Putting it all together

Here’ the sequence of code to place in your script to (1) create a temporary directory (if needed) and (2) generate a unique temporary filename specification (including path to the temporary directory). Within your script, you can write to the file or redirect output to the file from other scripts, command, cmdlets, etc. For this example, I’m prefacing the filename with “mytempfile” and making it of type “.csv” – this of course can be changed to whatever is appropriate for your script.

?View Code POWERSHELL
#create a temp directory if it does not already exist
if (!(test-path temp))
 {new-item temp -type directory}
 
#generate a unique file name (with path included)
$x = get-date
$script:WorkFile=[string]::format("temp\mytempfile_{0}_{1:d2}_{2:d2}_{3:d2}_{4:d2}_{5:d2}_{6:d4}.csv",
 $x.year,$x.month,$x.day,$x.hour,$x.minute,$x.second,$x.millisecond)

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 Posh TipsSuffusion theme by Sayontan Sinha

Page optimized by WP Minify WordPress Plugin