7

I am new to powershell. how do you over write file in powershell? write now below code is appending. I want to completely over write file with below new info.

$someinfo | Add-Content $FileName

NoStressDeveloper
  • 491
  • 3
  • 9
  • 26
  • 1
    Have a look on this https://stackoverflow.com/questions/10655788/powershell-set-content-and-out-file-what-is-the-difference#answer-10656021 – Shiko Jan 12 '18 at 00:30

2 Answers2

20

Use Set-Content instead of Add-Content.

Difference between Set-Content and Add-Content:
Add-Content=> Append
Set-Content=> replace content

Reference:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content?view=powershell-5.1

Shiko
  • 2,448
  • 1
  • 24
  • 31
7

You can do an Out-File as well

'Some content' | Out-File -FilePath c:\windows\temp\file.log
Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26