MURIMO
MURIMO

Reputation: 43

how to set filename that contains variable in powershell

i need to output Get-ChildItem cmdlet into file, which name contains current date. this is what is tried:

Get-Date -OutVariable date
Get-ChildItem > $date.txt

powershell doesn't print any error messsages, but there's no file created.

i need to do some equivalent to the following line in windows cmd

dir > %date%.txt

thanks in advance

Upvotes: 0

Views: 1932

Answers (1)

leeharvey1
leeharvey1

Reputation: 1436

Give this a try:

Get-Date -Format 'yyyy-MM-dd' -OutVariable date
Get-ChildItem | Out-File ".\$date.txt"

Keep in mind that Get-Date, by default, will include the time component, as well; which can contain colons (:) -- depending upon your regional time-formatting preferences -- which is an invalid character for filenames.

Hope this helps.

Upvotes: 1

Related Questions