Last updated on December 6, 2022
In this post, we gonna create a Text file using PowerShell. Using the New-Item
cmdlet we can create a text file and with the Set-Content
cmdlet, we can put content into it. Using get-Content
cmdlet we can read the generated file.
Create a text file
In this example, we’re creating a new text file named test.txt
Open your Powershell and execute below command
New-Item D:\temp\test.txt
You can see the test.txt
created in D:\temp
directory.
Add or Replace text in the file
In this example, we’re going to add content to the text file named test.txt
Open your PowerShell and execute below command
Set-Content D:\temp\test.txt 'Welcome to ArjunPHP.com'
You can see the test.txt
created or updated file in D:\temp
directory.
Set-Content
will replace the file if it already exists otherwise it will create and put content.
you can test it with the get-content
cmdlet will give the “Welcome to ArjunPHP.com” text output.
get-Content D:\temp\test.txt
Append data to existing file
Using the Add-Content cmdlet you can append data to a existing file at the end. If file not exists it will create file with given content.
Add-Content D:\temp\test.txt 'Welcome to ArjunPHP.com'