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
Share:
55,213

Related videos on Youtube

user358485
Author by

user358485

Updated on July 09, 2022

Comments

  • user358485
    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
      techie007 over 13 years
      echo This is My Text >> myText.txt? - Really though, this seems more suited to StackOverflow - voting to close/move...
  • mad
    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
    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
    Peter Mortensen almost 5 years
    An explanation would be in order.