Gazel
Gazel

Reputation: 265

in Powershell how to use two system variables to name a file?

I'm new to Powershell. I'm trying to export file to .csv file named with the computer name followed by time stamp. It works for me either with computername or timestamp, but not when both together. So far i have this:

enter code here$results | export-csv c:\$(get-content env:computername)$(get-date -f dd-MM-yyyy-HH-mm).csv

Upvotes: 0

Views: 13019

Answers (2)

Aaron Jensen
Aaron Jensen

Reputation: 26729

I like to use the -f operator. It makes the code a little easier to read. When using dates as timestamps, I luse a format that is YearMonthDayHourMinute, that way the file is sorted whenever you get a directory listing.

$results |
    Export-Csv -Path ('C:\{0}-{1:yyyyMMddHHmm}.csv' -f $env:COMPUTERNAME,(Get-Date))

Upvotes: 1

vonPryz
vonPryz

Reputation: 24071

The problem is caused by erroneus Get-Content- It would read contents from a file and is not used to read an environment variable. What you are looking for is more like

"c:\"+$env:computername+$(get-date -f dd-MM-yyyy-HH-mm)+".csv"
# Output:
c:\MyComputer03-07-2013-12-29.csv

Upvotes: 2

Related Questions