[英]How do I overwrite a file using PowerShell?
New-Item -Path "C:\aws" -Name "script.ps1" -ItemType "file" -Value "text in file"
我正在嘗試使用上述命令創建一個文件,如果該文件已經存在,我必須用新文件替換它,它將具有相同的文件名。 請幫忙
使用力——特別是-Force
參數...
New-Item -Path "C:\aws" -Name "script.ps1" -ItemType "file" -Value "text in file" -Force
無論script.ps1
是否已經存在,成功后它將包含text in file
。
New-Item
文檔的示例 #9中也演示了它的使用...
示例 9:使用 -Force 參數覆蓋現有文件 ======================================= ==========================
此示例創建一個具有值的文件,然后使用
-Force
重新創建該文件。 這會覆蓋現有文件,它會丟失它的內容,正如您通過長度屬性看到的那樣PS> New-Item./TestFile.txt -ItemType File -Value 'This is just a test file' Directory: C:\Source\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 5/1/2020 8:32 AM 24 TestFile.txt New-Item./TestFile.txt -ItemType File -Force Directory: C:\Source\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 5/1/2020 8:32 AM 0 TestFile.txt
在第二次調用New-Item
時省略-Force
會產生錯誤New-Item: The file '...\TestFile.txt' already exists.
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.