3

In a batchfile, how do I store the output of a powershell command in a variable This isn't working

set yest=powershell get-date((get-date).addDays(-1)) -uformat "%Y%m%d"


powershell get-date((get-date).addDays(-1)) -uformat "%Y%m%d"

gives 20130623

set yest=powershell get-date((get-date).addDays(-1)) -uformat "%Y%m%d"
echo %yest%

gives powershell get-date((get-date).addDays(-1)) -uformat "md"

4
  • Are you using echo in powershell or are those two different scripts / consoles? Commented Jun 25, 2013 at 1:40
  • echo is in a batch file.. I'm using powershell in a batch file Commented Jun 25, 2013 at 1:40
  • Actually, looks like this was already answered: stackoverflow.com/questions/2955512/… Commented Jun 25, 2013 at 1:58
  • @AthomSfere It works from command line. But when I put it in a batch file, it gives me an error a was unexpected at this time Commented Jun 25, 2013 at 2:03

1 Answer 1

8

The closing braces and percent need to be escaped/doubled.

@echo off
for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^) -uformat "%%Y%%m%%d"') do set d8=%%a
echo %d8%
pause
Sign up to request clarification or add additional context in comments.

3 Comments

@Jubjub Bandersnatch No, that's not true - it doesn't use backticks and works as posted.
Right you are. I just assumed those were backticks because I always use them.
whats with the for /f?

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.