说明
遇到一个powershell Here string 中的换行致使的坑,这里来验证下不一样版本中powershell here string 的行为。若是你不有心注意,极可能踩坑。shell
PS1 中的here string 表现
- 不能使用空的here string ,不然你会发如今console 中敲入屡次回车,这个here string 都闭合不了。
- 若是here string 中有换行,换行会被吃掉。

PS2 中的here string 表现
- 能够使用空的here string
- 若是here string 中有换行,换行会被吃掉。

PS3 中的here string 表现
同PS2,验证方法同PS2ide
PS4 中的here string 表现
同PS2,验证方法同PS2post
PS5 中的here string 表现
- 能够使用空的here string
- 若是here string 中有换行,换行会被保留。
- 换行符是\n ,而不是\r\n 也就是CRLF,这点在特定状况下影响很大(好比http Post 的数据的换行符明确要求是CRLF,少个\r ,你可能post 会出错的。)

总结
- 若是要在powershell here string 中保留换行,那么显示的方式是直接写成下面
$a=@"
`r`n
aa
"
可是你会发现,powershell 在多个版本中会给你再多加一个\ncode
PS C:\> $a=@"
>> `r`n
>> aa
>> "@
>>
PS C:\> $a.length
5
PS C:\> $a=@"
>> `r`n
>> aa
>> "@
>>
PS C:\> $a.legnth
PS C:\> $a.length
5
PS C:\> $b=$a -replace '(\r\n|\r|\n)',"`r`n"
PS C:\> $b.length
6
PS C:\> $b
aa
PS C:\>