Script to make content entry into a text file
55,213
Solution 1
I will give you an all PowerShell answer. You can use Add-Content or Set-Content cmdlets.
Set-Content overwrites the target file and Add-Content appends to the file.
Set-Content -Value "Test1" -Path C:\Scripts\Scratch\test.txt
Add-Content -Value "Test" -Path C:\Scripts\Scratch\test.txt
Or, you can also use Out-File.
"Test" | Out-File -FilePath C:\Scripts\Scratch\test.txt -Append
Solution 2
The command you need is echo
(alias of Write-Output - use Get-Alias to get the list):
echo Text >> textFile.txt
This link should prove helpful in learning Windows commands.
Solution 3
Here is the sample code to create and add content into a text file:
$text = Hello World
# This is to create file:
$text | Set-Content MyFile.txt
# Or
$text | Out-File MyFile.txt
# Or
$text > MyFile.txt
# This is to write into a file or append to the text file created:
$text | Add-Content MyFile.txt
# Or
$text | Out-File MyFile.txt -Append
# Or
$text >> MyFile.txt
Related videos on Youtube
Author by
user358485
Updated on July 09, 2022Comments
-
user358485 almost 2 years
How do I include some "text" into a .txt format file without opening the same via a script on Windows?
-
techie007 over 13 yearsecho This is My Text >> myText.txt? - Really though, this seems more suited to StackOverflow - voting to close/move...
-
-
mad over 7 years$arr =@("jind",12, "singh") write-host $arr[1] read-host $arr += "reza" write-host $arr[3] read-host write-host $arr[$arr.length-1] read-host $arr = $arr -ne $arr[1] write-host $arr read-host foreach ($i in $arr) {write-host $i}
-
mad over 7 years$room = Read-Host “Enter a room number:” get-content computers.txt | where {$_ -match "B108"} $newcontents = get-content computers.txt | where {$_ -match "B108"}
-
Peter Mortensen almost 5 yearsAn explanation would be in order.