How do I include some "text" into a .txt format file without opening the same via a script on Windows?
Asked
Active
Viewed 5.6k times
23
-
1echo This is My Text >> myText.txt? - Really though, this seems more suited to StackOverflow - voting to close/move... – techie007 Feb 25 '11 at 15:51
6 Answers
37
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

Peter Mortensen
- 30,738
- 21
- 105
- 131

ravikanth
- 24,922
- 4
- 60
- 60
5
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

Peter Mortensen
- 30,738
- 21
- 105
- 131

Jeff D
- 349
- 3
- 4
5
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.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Wipqozn
- 1,282
- 2
- 17
- 30
0
If you want to do it interactively from a standard Windows command prompt (typing the content in at the keyboard), you can use the following:
copy con c:\temp\file.txt
Then you can just start typing. To finish, just hit Ctrl+Z and ENTER, like so:
Hello world!
Goodbye...^Z
1 file(s) copied.
To view the file, use:
type c:\temp\file.txt
You should see the following output:
Hello world!
Goodbye...

G-Mac
- 1,173
- 13
- 10
0
The Get-Content cmdlet should work fine for you.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Bill
- 5,263
- 6
- 35
- 50
-4
$com1 = New-Object PSobject # Task 1
$com2 = New-Object PSobject # Task 1
$com3 = New-Object PSobject # Task 1
$com1 | Add-Member noteproperty -name user -value jindpal # Task 2
$com1 | Add-Member noteproperty -name code -value IT01 # Task 2
$com1 | Add-Member scriptmethod ver {[system.Environment]::oSVersion.Version} # Task 3
$com2 | Add-Member noteproperty -name user -value singh # Task 2
$com2 | Add-Member noteproperty -name code -value IT02 # Task 2
$com2 | Add-Member scriptmethod ver {[system.Environment]::oSVersion.Version} # Task 3
$com3 | Add-Member noteproperty -name user -value dhanoa # Task 2
$com3 | Add-Member noteproperty -name code -value IT03 # Task 2
$com3 | Add-Member scriptmethod ver {[system.Environment]::oSVersion.Version} # Task 3
$arr += $com1, $com2, $com3 # Task4
Write-Host "Windows version of computer 1 is: "$com1.ver() # Task 3
Write-Host "User name of computer 1 is: "$com1.user # Task 6
Write-Host "Code of computer 1 is: "$com1,code # Task 5
Write-Host "Windows version of computer 2 is: "$com2.ver() # Task 3
Write-Host "User name of computer 2 is: "$com2.user # Task 6
Write-Host "Windows version of computer 3 is: "$com3.ver() # Task 3
Write-Host "User name of computer 3 is: "$com1.user # Task 6
Write-Host "Code of computer 3 is: "$com3,code # Task 5
Read-Host

Peter Mortensen
- 30,738
- 21
- 105
- 131

mad
- 1
-
$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 Oct 13 '16 at 00:31
-
$room = Read-Host “Enter a room number:” get-content computers.txt | where {$_ -match "B108"} $newcontents = get-content computers.txt | where {$_ -match "B108"} – mad Oct 13 '16 at 00:40
-