This article describes how to output a string to the end of a file file in PowerShell, which does not overwrite the original content, but only after the original text is appended.
There is a task: the "This is powershell!" This phrase is appended to the D:\1.txt file.
In the face of this task, if we immediately to think FileStream object, that is wrong! FileStream is the traditional method in. Net! In PowerShell, we can use the Out-file cmdlet to step through the effect of attaching a string to a file. And look at the following demo:
Copy Code code as follows:
PS c:\users\spaybow> Type D:\1.txt
Hello world!
PS c:\users\spaybow> "This is PowerShell!" | Out-file-append D:\1.txt
PS c:\users\spaybow> Type D:\1.txt
Hello world!
This is PowerShell!
Explain:
The first command, using type to display the contents of the file. Type is an alias for Get-content.
The second command, using the Out-file this cmdlet, puts "This is PowerShell!" As a pipe parameter input, output into the d:\1.txt. Here you use a-append parameter, which means that you attach the input to the end of the original text without overwriting the original text. If we do not use the-append parameter, the third command will display the result with only one sentence "This is PowerShell!".
About PowerShell append string to file, this article introduces so many, hope to be helpful to you, thank you!