I'm pretty new to Powershell and would like to output the result of a command to a file name. I try the following within the powershell shell:
$a = (Get-Date).DayofYear
$b = get-date -format "yy"
$date = "$b$a"
echo $date
This works fine.
But I'd like to have a cmd-file with the output »$date.txt«. How to proceed?
Thanks for all help in advance.
This should do the trick:
$a = (Get-Date).DayofYear
$b = get-date -format "yy"
$date = "$b$a"
$date | out-file -FilePath "$date.txt"
... or depending on preference
$date | out-file -FilePath ($date + ".txt")
As per your comments, you can do this from CMD like so:
powershell.exe "((Get-Date).DayofYear).ToString() + (get-date -format 'yy').ToString()| % {$_ |Out-File ($_ + '.txt')}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.