# Add text to a file

**URL:** <https://forums.powershell.org/t/add-text-to-a-file/23965>\
**Category:** PowerShell Help\
**Created:** [April 17, 2024, 1:33pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965 "2024-04-17T13:33:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gloops](https://avatars.discourse-cdn.com/v4/letter/g/8491ac/32.png) [@Gloops](https://forums.powershell.org/u/Gloops)\
**Post date:** [April 17, 2024, 1:33pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/1 "2024-04-17T13:33:48Z")

</div>

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?

```auto
$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”

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [April 17, 2024, 1:54pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/2 "2024-04-17T13:54:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gloops](https://avatars.discourse-cdn.com/v4/letter/g/8491ac/32.png) [@Gloops](https://forums.powershell.org/u/Gloops)\
**Post date:** [April 17, 2024, 2:09pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/3 "2024-04-17T14:09:58Z")

</div>

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

```auto
"This is a text string." | Out-File -Append testfile1.txt

```

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [April 17, 2024, 3:20pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/4 "2024-04-17T15:20:18Z")

</div>

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.

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![Gloops](https://avatars.discourse-cdn.com/v4/letter/g/8491ac/32.png) [@Gloops](https://forums.powershell.org/u/Gloops)\
**Post date:** [April 17, 2024, 3:44pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/5 "2024-04-17T15:44:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [April 17, 2024, 3:57pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/6 "2024-04-17T15:57:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [April 18, 2024, 1:28pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/7 "2024-04-18T13:28:25Z")

</div>

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.

```auto
 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](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines)

---

<div class="post-metadata">

**Author:** ![Gloops](https://avatars.discourse-cdn.com/v4/letter/g/8491ac/32.png) [@Gloops](https://forums.powershell.org/u/Gloops)\
**Post date:** [April 18, 2024, 4:18pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/8 "2024-04-18T16:18:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [April 19, 2024, 1:33pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/9 "2024-04-19T13:33:07Z")

</div>

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)

> **[about Automatic Variables - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.4)**
>
> Describes variables that store state information for PowerShell. These variables are created and maintained by PowerShell.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 19, 2024, 1:33pm UTC](https://forums.powershell.org/t/add-text-to-a-file/23965/10 "2024-05-19T13:33:31Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
