I am using the following PS cmdlets to create a new json file every time it is executed. The existing json file should be overwritten
$jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
$jsonformatOutput | New-Item -path $myFileName -Force
However, a new json file is not created if their is already an existing one with the same filename.
New-Item
isn't the function to choose for this situation (since it literally should only be used to create New-Items).
What you should use instead, is Out-File
$jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
$jsonformatOutput | Out-File -Filepath $myFileName
This writes the Variable to the file $myFileName
and overwrites the file if it still exists.
If you want to add content to an existing file instead of overwriting it, you can use -Append
.