PowerShell method of using Out-file to save string or run results to a file _powershell

Source: Internet
Author: User
Tags string to file

This article describes how to output a string to a file file in PowerShell.

There is a task: To quickly create a 1.txt file under D:\, and write a phrase "Hello world! ”。

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 achieve this effect in one step.

Copy Code code as follows:

PS c:\users\spaybow> "Hello world!" | Out-file D:\1.txt
PS c:\users\spaybow> Type D:\1.txt
Hello world!

Explain:
The first command, using Out-file this cmdlet, put "Hello world!" As a pipe parameter input, output into the d:\1.txt. If no matter how written, regardless of whether the file exists, regardless of encoding problem, then directly out-file < filename > can output a string to a file.
The second command, using type to display the contents of the file. Type is an alias for Get-content.

About PowerShell output string to file, this article on the introduction so much, I hope to help 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.