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!