Whenever you write information to disk, it makes sense to use unique temporary file names. If you use static file names and run your code more than once, the same file would be overwritten time after time. If someone opened the file and locked it, this could cause your script to fail.

Here are some easy approaches to generate temporary file names that are unique:

# use a random number (slight chance of duplicates)
$path = "$env:temp\liste$(Get-Random).csv"
"Path is: $path"

# use a GUID. Guaranteed to be unique but somewhat hard on the human eye
$path = "$env:temp\liste$([Guid]::NewGuid().toString()).csv"
"Path is: $path"

# use timestamp with milliseconds
$path = "$env:temp\liste$(Get-Date -format yyyy-MM-dd_HHmmss_ffff).csv"
"Path is: $path"

Twitter This Tip! ReTweet this Tip!

Anonymous