【问题标题】:Store powershell output in a variable in CMD将 powershell 输出存储在 CMD 中的变量中
【发布时间】:2013-06-21 16:11:42
【问题描述】:

在批处理文件中,如何将 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 还是这两个不同的脚本/控制台?
  • echo 在批处理文件中。我在批处理文件中使用 powershell
  • 其实这个好像已经回答了:stackoverflow.com/questions/2955512/…
  • @AthomSfere 它从命令行工作。但是当我把它放在一个批处理文件中时,它给了我一个错误a was unexpected at this time

标签: powershell windows-7 batch-file cmd


【解决方案1】:

右大括号和百分比需要转义/加倍。

@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

【讨论】:

  • @Jubjub Bandersnatch 不,这不是真的 - 它不使用反引号并按发布的那样工作。
  • 没错。我只是假设那些是反引号,因为我总是使用它们。
猜你喜欢
  • 1970-01-01
  • 2012-07-19
  • 2019-10-27
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
相关资源
最近更新 更多