0

I would like to achieve what I can do in a PowerShell CLI also within a Batch-Script:

PS C:\Users\andreas.luckert> $timestamp = Get-Date -UFormat "%d-%m-%Y--%R-UTC%Z" | ForEach-Object { $_ -replace ":", "." }

PS C:\Users\andreas.luckert> echo $timestamp
26-11-2021--15.55-UTC+01

Now, within my Batch-Script, I tried approaches similar to the following

SET _timestamp=('Get-Date -UFormat "%d-%m-%Y--%R-UTC%Z" | ForEach-Object { $_ -replace ":", "." }')

Yet, it does not work.

Solutions like this look a bit hacky to me, the general instructions for batch variables does not help in this case and all of these approaches are very ugly syntax-wise in comparison to the nice and clean PowerShell command I mentioned in the very beginning. Moreover, none of them include the timezone, which is important to me.

Andreas L.
  • 3,239
  • 5
  • 26
  • 65
  • The syntax may be ugly to capture the output of a command to assign to a variable but your answer was definitely in the last question you linked to and you will find dozens more questions and answers with that same syntax. – Squashman Nov 26 '21 at 16:41
  • The answer of @mklement0 below has proven that there is a way to utilize PowerShell commands which allows for employing exactly the same syntax one is used to. – Andreas L. Dec 01 '21 at 18:51
  • The syntax that mklement0 used in his answer is the same syntax used in this [answer](https://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script/19163883#19163883) from the [third question](https://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script) you linked to in your original question. You basically only have two options for capturing output of a command in a batch file. You either use the `FOR /F` command or you write to a file and read it back with a `SET /P` or another `FOR /F` command. – Squashman Dec 01 '21 at 19:30
  • While it is true that some of the alternative answers in the other question might have worked, I'm new to PowerShell commands and wouldn't have known how to adapt said answer to my specific need. Therefore, I'm grateful that @mklement0 constructed it in his answer to my question. – Andreas L. Dec 02 '21 at 13:23

1 Answers1

4
  • You need to call powershell.exe, Windows PowerShell's CLI, in order to execute a PowerShell command from a batch file - note that such a call is expensive.

  • You need to parse the output via a for /f loop in your batch file; run for /? for help from a cmd.exe session (Command Prompt).

  • You need to double % characters that the batch file should treat verbatim.

To put it all together:

@echo off

for /f "usebackq delims=" %%i in (`
  powershell -c "(Get-Date -UFormat '%%d-%%m-%%Y--%%R-UTC%%Z') -replace ':', '.'"
`) do set _timestamp=%%i

echo %_timestamp%

Note: Consider placing -noprofile before -c to suppress loading of PowerShell's profiles, for better performance and a predictable execution environment.

mklement0
  • 382,024
  • 64
  • 607
  • 775