在CMD中将PowerShell输出存储到变量中

3
在批处理文件中,如何将PowerShell命令的输出存储到变量中? 这个方法不起作用。
set yest=powershell get-date((get-date).addDays(-1)) -uformat "%Y%m%d"


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

返回 20130623

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

执行powershell get-date((get-date).addDays(-1)) -uformat "md"命令可获取昨天的日期,格式为月日。


你是在PowerShell中使用echo命令,还是这两个是不同的脚本/控制台? - Austin T French
echo 命令在批处理文件中。我正在批处理文件中使用 PowerShell。 - Ank
实际上,看起来这个问题已经有人回答了:https://dev59.com/D07Sa4cB1Zd3GeqP1Ssv - Austin T French
@AthomSfere 它可以从命令行运行。但是当我将它放入批处理文件中时,它会给我一个错误提示:“a在此处是意外的”。 - Ank
1个回答

8
结束大括号和百分号需要进行转义/加倍处理。
@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

2
@Jubjub Bandersnatch 不,那不是真的 - 它不使用反引号并且按照发布的方式工作。 - foxidrive
没错,我只是假设那些是反引号,因为我总是使用它们。 - mojo
for /f 是什么意思? - john k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接