簡體   English   中英

如何使用 PowerShell 覆蓋文件?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM