Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用添加内容PowerShell向现有文件的新行添加文本_Powershell_Powershell 2.0_Powershell 4.0 - Fatal编程技术网

使用添加内容PowerShell向现有文件的新行添加文本

使用添加内容PowerShell向现有文件的新行添加文本,powershell,powershell-2.0,powershell-4.0,Powershell,Powershell 2.0,Powershell 4.0,我正在尝试使用get content从文件中获取内容,并使用add content将其添加到现有文件中,但问题是我无法将该内容添加到新行。你能就此提出建议吗 使用以下命令: Get-content -Path $destRoot1 | Add-Content -Path $destRoot2 您需要将内容设置为目标文件路径,而不是添加内容。请参阅以下更改: $destRoot1 = "C:\File\path1.txt" $destRoot2 = "C:\des

我正在尝试使用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 #####

如果文件还不存在,您将需要稍微不同的语法,我想您想添加$destRoot1和app end到$destRoot2的特定行?@FletcherF1我想添加从$destRoot1到$destRoot2的所有行,但是从第二行开始,它还应该覆盖从第二行到文件末尾的以前的内容。第一行应该保持不变。@DavidMartin文件已经存在,因为我只需要覆盖第2行的内容。第一行是常量。在附加另一个文件之前运行
“”|添加内容…
?我不想设置它,因为我需要从第二行附加它$$destRoot2包含我不想删除的标题,需要在第二个标题之后添加数据line@Divya:Ya,然后只需使用-Append向现有文件添加行。