Add text to a file

Hello everybody,
I need to append text to a file.
I tried with New-Item, and I was answered that there is an error, as the file already exists.

Well, if I want to add text to a file, is not it mandatory that it already exists?

$n="List - Copie ($i).txt";	
$n;
New-Item -Path . -Name "testfile1.txt" -ItemType "file" -Value "This is a text string."

Oh, I realize that the beginning is an illustration of something I did not apply. $n is an example of something I shall append to the file. But I presume that if PowerShell fails to write “This is a text string”, it will also fail to write “List - Copie (7).txt”

I suggest you look at the Out-File command as a way to write lines of text to a file.

There are other options if you are exporting structured data to a file (Export-Csv for example), but for your example, Out-File should do the trick.

And Out-File has an -Append parameter to add to an existing file.

Thank you.
So, it seems the syntax will be:

"This is a text string." | Out-File -Append testfile1.txt
1 Like

New-Item, as the verb implies, is for creating new items. Files and folders specifically. If you run Get-Help against New-Item you’ll see it also has a -Force parameter which can be used to force an overwrite of an already existing file. I wouldn’t recommend this for your use case.
Out-File with the -Append parameter is most likely what you want. There are also cmdlets ending in *-Content that might be of interest that you should be aware of, but I don’t think they’re what you want here.

Get-Command *-Content

Additionally, with Out-File you do not need to create the file first if it doesn’t exist. The cmdlet will handle creating the file. Therefore your line could turn into:

"This is a text string" | Out-File -FilePath .\testfile1.txt -Append  
# or  
Out-File -FilePath .\testfile1.txt -InputObject "This is a text string" -Append

Yes, that is what I did, and it works fine.
I should have guessed it would be better to use an absolute path.
Hum … A relative one is also good. I was about to say the absolute path had a problem, but … no, it was in the loop that the source file had only one entry, so it was normal not to see any other.

I launched it several times with larger sources, and it works fine.
The absence of a content parameter is a curiosity, but with the tunnel it works OK.

I’m not sure what you mean by “absence of a content parameter is a curiosity.” Out-File uses the -InputObject parameter to supply input to it. New-Item, by comparison uses the -Value parameter and Add-Content uses -Value.

And yes, in almost all cases and all shells, it’s always better to be specific and use the full file path to an item rather than relative. Relative is, well, relative. You never know when you’ll have some weird scenario that changes that.

I’m guessing by “tunnel” you mean the pipeline.

Piping your text into the command makes these two do the same thing, as @grey0ut said earlier.

 Out-File -FilePath .\testfile1.txt -InputObject "This is a text string" -Append
"This is a text string" | Out-File -FilePath .\testfile1.txt -Append  

You might read this for more information about pipelines. They are very useful but can be confusing.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines

I meant pipeline, yes, right.
Thank you for the documentation.
Oh, I did not see the parameter -InputObject (which can be a string I imagine), this was the meaning of my remark.
I do not remember what happened with the absolute path exactly, maybe I doubled the ‘\’ as I should not, or reverse.
The advantage of working in the same directory as the input file is that designating the path is somewhat more simple. As when you go to the shop you do not need to give your address and explain to the delivery driver where he can park.

For the path, this might also be helpful: $PSScriptRoot, if your script is run from the location where you are working, or in a path relative to where your working files are. ($PSScriptRoot\workingdir\file1.txt)