PowerShell out-file Append string to end of file _powershell

Source: Internet
Author: User
Tags string to file

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.