The Add-Content
cmdlet is used to write content into a file. It appends the content into a file if it exists. It creates the file if doesn't exist.
Add-Content -Path "path to file" -Value "new content"
The function takes the following mandatory parameters:
Path
: We provide the path to the file.Value
: We provide content to this parameter.It appends the content provided by the Value
parameter to the file provided by the Path
parameter.
#!/usr/bin/pwsh -Command#Write content to a fileSet-Content -Path "./data.txt" -Value "This is written by Set-Content cmdlet `n"#Append content to a fileAdd-Content -Path "./data.txt" -Value "This is written by Add-Content cmdlet `n"#Read content from a fileGet-Content -Path "./data.txt"
Set-Content
cmdlet.data.txt
, using the Add-Content
cmdlet, to which Set-Content
cmdlet also wrote.data.txt
, using the Get-Content
cmdlet.From the output, we can see that the content written by Set-Content
is not overwritten by the Add-Content
cmdlet.