HHH
HHH

Reputation: 6465

create a file name using time in Powershell

I'm pretty new to Powershell and would like to output the result of a command to a file which has a timestamp. Currently I could get the date but I can't get the time. Here is what I have:

mstest /testcontainer:"C:\CodedUITests\CodedUISP.dll" /resultsfile:"C:\CodedUITests\TestResults\result $(get-date -f yyyy-MM-dd)$.trx"

How can I get the time as well?

Upvotes: 0

Views: 96

Answers (2)

Nick H.
Nick H.

Reputation: 1616

I'm doing almost the same thing! Here's my code:

        $outputFile = "$tempLocation\work\$($solutionName)_$((Get-Date -Format s).ToString().Replace('-','').Replace(':','')).trx"

        Write-Verbose "$($MyInvocation.MyCommand.Name): Running MSTest.exe..." 
        Invoke-ExternalCommand MSTest.exe @(
            "/testcontainer:$testContainer",
            "/resultsfile:$outputFile")

```

Invoke-ExternalCommand is just an abstracted function that calls the mstest command. Doing this allows me to mock the call making Pester testing easier.

This is what the trc file is named: SolutionName_20151015T113206.trx

Upvotes: 1

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

After a quick google search I found this reference:

Get-Date -f yyyy-MM-dd-HH-mm-ss

Upvotes: 1

Related Questions