Tuesday, February 5, 2013

Powershell Script DIY: Unique Temp File names

 Hello again!

Have you ever need a unique temp filename?  A name that you can easily call and manipulate?  First you start with the variable:


$foo can be anything.  it can be $Jamaca, or even $US_of_A.  All that really matters is that it’s easy to pick out in your script and it begins with a $.  All variables in Powershell begin with a $.

Next we move on to assigning a variable.

$foo = "foo"

Of course you probably don’t want your variable to have a value of "foo" but foo can be anything, even:

$foo = "c:\Temp\TEMP"

This is only telling $Foo what it is.  All that matters is that it has quotes.

After that we want to get it a unique string:

$foo = "c:\Temp\TEMP-$(Get-Date -format 'yyyy-MM-dd hh-mm-ss').log"

Here is the complicated part.  We are using the system Date Time Group to generate a unique identifier.

The TEMP portion is giving the string a beginning, and after that we begin with the variable portion (anything between will show up in the name.) The Get-Date function will pull the system time at that second.  But wait, everyone formats their date differently!  Not to worry, here is a quick reference guide:

dd - 2 digit day of the month
ddd - 3 character day of the week (i.e. tuesday)
dddd - full day name (i.e. Tuesday)
mm - 2 digit month
yyyy - 4 digit year
yy - 2 digit year
hh - hour (12 hour format)
HH - hour (24 hour format)
MM - Minuet
ss - Second

Now I perfer the format yyyy-MM-dd hh-mm-ss, and the dashes are only delimiters, they are not necessary.  you could even have:

$foo = "c:\Temp\TEMP-$(Get-Date -format 'dddd-dd-mm-yyyy HH-MM-ss').log"

This will get you a output of "TEMP-Tuesday-05-39-2013 14-02-25.log"

Make sure the format has single quotes around it.  Make sure everything from before Get-date to the end of your date format is in parentheses. 

Now you add the extension, while this is not necessary, I find it useful for debugging output.  My personal favorite is .log but you can name it anything.  Keep in mind that windows may not like it if you name something .dll or something of that nature.  

To complete your variable you need to encapsulate it in quotes:

$foo = "c:\Temp\TEMP-$(Get-Date -format 'yyyy-MM-dd hh-mm-ss').log"

Just the value, not the variable itself.

After that you need to create the file itself.  Start with New-Item.  Self explanatory but easily overlooked.  Append the variable name, in quotes after it so it looks like this:

New-Item "$foo"

This will prompt Powershell to ask you what type of item you want.  It can be a folder, a file, ECT.

To avoid this, you need to select an item type with, you guessed it, -itemType.  For the purpose of this exercise, I selected File for my item type.  don’t forget the - as it denotes it as a switch. The command should look like this:

New-Item "$foo" -ItemType File

Last but not least you need to roll it all together for script friendly formatting:

$foo = "c:\Temp\TEMP-$(Get-Date -format 'yyyy-MM-dd HH-mm-ss').log"
New-Item "$foo" -itemType File 

And that my friends, Is how it’s done.  Do you have another way to do it? Do you have comments? Leave them below!

Sources: Unknown Internet Source

No comments:

Post a Comment