1

The following command line does not work in a Windows batch file:

Powershell.exe -Command Get-Date -Format 'yyyy-MM-dd HH:mm:ss' echo "Hello World"

The expected result is: 2021-05-09 12:00:00 Hello World

But PowerShell outputs the error message:

Get-Date : Cannot bind parameter 'Date'. Cannot convert value "echo" to type "System.DateTime".
Error: "The string was not recognized as a valid DateTime. There is a unknown word starting at index 0."
At line:1 char:9
+ Get-Date <<<<  -Format 'yyyy-MM-dd HH:mm:ss' echo Hello World
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

How to get output of the PowerShell command to get date and time in format yyyy-MM-dd HH:mm:ss and of the command ECHO in Windows batch file output on one line?

2
  • Does this answer your question? Batch Echo Style: two statements into same line Commented May 9, 2021 at 8:31
  • No. It doesn't. In this command, I have called Powershell command in cmd. But at the same time, I need to use ECHO command as well. And the question is how to combine two command into one line? I have no idea add what syntax or parameter to implement on it. Commented May 9, 2021 at 8:53

2 Answers 2

5

The command line to use in a Windows batch file is:

@for /F "usebackq delims=" %%I in (`%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -Command Get-Date -Format 'yyyy-MM-dd HH:mm:ss'`) do @echo %%I Hello World

The Windows command processor cmd.exe processing the batch file starts one more cmd.exe in background with option /c and the command line specified within set of command FOR as additional arguments. The second cmd.exe instance runs powershell.exe to get local date time and outputs it in the specified format. This output of PowerShell written to handle STDOUT (standard output) of background command process is captured by the command process which is processing the batch file and is processed after started cmd.exe terminated itself by the cmd internal command FOR.

The output line is assigned as is to the loop variable I because of using delims= to define an empty list of string delimiters and the fact that the line does not start with a semicolon which is the default end of line character. The string value assigned to loop variable I is output with the text by the command ECHO to handle STDOUT of the command process which is processing the batch file.

Open a command prompt, run for /? and read the output help carefully and completely from top of first to bottom of last page.

See also the answers on:

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

Comments

3

You could of course just do it like this:

PowerShell Write-Host (Get-Date).ToString('yyyy-MM-dd HH:mm:ss') Hello World

Although, as this is from a pre-written batch-file, I'd advise that you use full paths and more readable and understandable syntax:

%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -Command "Write-Host (Get-Date).ToString('yyyy-MM-dd HH:mm:ss') Hello World"

2 Comments

It seems to me that your answer fits more the question as OP doesn't ask to retrieve the date in an env variable but to display the date alongside the output of a batch echo command. Why not using then the 'escaping' way like Powershell.exe -Command Get-Date -Format 'yyyy-MM-dd HH:mm:ss' ^| Write-Host -NoNewline & echo "Hello World" ?
TBF, there are many ways of achieving it @Zilog80, but for me, that is less basic, especially for the OP's example, and I didn't want this to be an answer about how to use PowerShell.

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.