cmdlet作为另一个的输入

时间:2015-04-29 10:17:29

标签: powershell

我有以下PowerShell命令替换文件:

(Get-Content c:\myfile.txt) | ForEach-Object {
  $_ -replace "date is x", "date is 2015.02.29"
} | Set-Content c:\myfile.txt

我希望硬编码日期(2015.02.29)来自另一个cmdlet:

(Get-Date -format yyyy.MM.dd)

我该如何注射?

1 个答案:

答案 0 :(得分:2)

只需用您的命令替换您的硬编码值:

(Get-Content c:\myfile.txt) | ForEach-Object {
  $_ -replace "date is x", "date is $(Get-Date -format yyyy.MM.dd)"
} | Set-Content c:\myfile.txt