【问题标题】:PS is not creating a new file everytimePS不是每次都创建一个新文件
【发布时间】:2018-08-10 02:48:55
【问题描述】:
我每次执行时都使用以下 PS cmdlet 创建一个新的 json 文件。应该覆盖现有的 json 文件
$jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
$jsonformatOutput | New-Item -path $myFileName -Force
但是,如果它们已经是具有相同文件名的现有文件,则不会创建新的 json 文件。
【问题讨论】:
标签:
windows
powershell
powershell-4.0
powershell-remoting
【解决方案1】:
New-Item 不是针对这种情况选择的函数(因为它实际上只能用于创建 New-Items)。
你应该改用Out-File
$jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
$jsonformatOutput | Out-File -Filepath $myFileName
这会将变量写入文件$myFileName,如果该文件仍然存在,则覆盖该文件。
如果您想将内容添加到现有文件而不是覆盖它,您可以使用-Append。