0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it did! thanks a lot!! i should have tried tinkering with quotes. Get-Date -OutVariable date Get-ChildItem > ".\$date.txt" also works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.