使用 Add-Content PowerShell 将文本添加到现有文件的新行

Adding text to a new line of existing file using Add-Content PowerShell

我正在尝试使用 get-content 从文件中获取内容并使用 add-content 将其添加到现有文件,但问题是我无法将该内容添加到新行。你能对此提出建议吗?

使用以下命令:

Get-content -Path $destRoot1 | Add-Content -Path $destRoot2 

您需要将内容设置为目标文件路径,而不是添加内容。请参阅以下更改:

$destRoot1 = "C:\File\path1.txt"
$destRoot2 = "C:\destination\path\of\the\file.txt"
Get-content -Path $destRoot1 |Set-Content -Path $destRoot2 -Force

我已经通过以下技巧解决了这个问题:

1.Add-Content -Path $destRoot2 "`n" 
##### I am adding new line character to existing file #####
2.Get-content -Path $destRoot1 | Add-Content -Path $destRoot2 -Encoding UTF8
##### Appending data from $destRoot1 to $destRoot2 #####
3.((Get-Content -Path $destRoot2) -notmatch '^\s*$' )| Select-Object -Unique | Set-Content -Path $destRoot2 -Encoding UTF8 
##### Using -notmatch '^\s*$' removing the blank line which I have added at line number 1 #####