首页 > 解决方案 > 使用命令提示符将 PowerShell 脚本的输出重定向到文件

问题描述

如何在 Windows 10 中使用 cmd 将 PowerShell 5.0 脚本的输出重定向到文件?我尝试了以下方法:

powershell ".\myscript.ps1 | Out-File outfile.txt"

和:

powershell .\myscript.ps1 > outfile.txt

无济于事。已outfile.txt创建但仍为空。命令提示符窗口以管理员权限运行。

在我使用的脚本中:

Write-Host $MyVar
Write-Host $SomeOtherVar

将值输出到屏幕。

标签: powershellcmd

解决方案


使用特定的PowerShell 重定向运算符:(
它们似乎也可以在 (cmd) 命令提示符下工作)

重定向成功流(1>):

powershell .\myscript.ps1 1> outfile.txt

重定向所有流 ( *>):

powershell .\myscript.ps1 *> outfile.txt

推荐阅读