将新行从命令行传递到PowerShell

将新行从命令行传递到PowerShell,powershell,powershell-3.0,powershell-4.0,Powershell,Powershell 3.0,Powershell 4.0,如何将新行字符从命令行传递到PowerShell // MyScript.ps1: [CmdletBinding()] Param( [Parameter()] $Param ) Write-Host $Param // Command line (not PowerShell - cmd.exe!) powershell.exe -File MyScript.ps1 -Param "First`nSecond" 不起作用。另外,使用命令行的新行字符\n也不起作用。那么如何将新行传

如何将新行字符从命令行传递到PowerShell

// MyScript.ps1:
[CmdletBinding()]
Param(
    [Parameter()] $Param
)

Write-Host $Param

// Command line (not PowerShell - cmd.exe!)
powershell.exe -File MyScript.ps1 -Param "First`nSecond"

不起作用。另外,使用命令行的新行字符
\n
也不起作用。那么如何将新行传递到命令行?

您需要回车和换行:

`r`n
因此,这应该是可行的:

powershell.exe -File MyScript.ps1 -Param "First`r`nSecond"

解决方法是在命令行中使用任何其他字符,例如
\n
,并在PowerShell中替换它:

$x = $param.Replace("\n", "`n")

这是可行的,但是,这当然是一种黑客行为,而不是我的首选解决方案。

有一个特殊字符,如
第一行所示◙第二行

谢谢,但是,Write Host不会添加新行,而是输出
r
n。另外,如果我将参数传递给
convertfromstringdata
它无法识别新行…嗯,我尝试了脚本(将其保存为MyScript.ps1),它将两行正确输出字符串。你能不能试着写主机“First
r
nSecond”@jisaak现在试试raw
cmd.exe
。如果您在Powershell提示符下键入该命令,而不是在cmd.exe提示符下键入该命令,则该脚本确实有效。啊,我错过了在
replace
调用中复制backtick时可以使用的部分(backtick n)
$x=$param.Replace(“`n”,“
n”)`这似乎是一些特殊的新行字符,powershell和命令行都不知道它。它确实适用于
Write Host
,但在其他方面与“backtick n”不同,例如
ConvertFrom-StringData
之类的cmdlet不接受它。遗憾:-(