PowerShell New-Item用於在檔系統中創建檔和文件夾,還在註冊表中創建註冊表項和條目。 創建檔後,它還將初始內容添加到檔中。
語法
語法1
New-Item
[-Path] <string[]>
[-ItemType <string>]
[-Value <Object>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[<CommonParameters>]
語法2
New-Item
[[-Path] <string[]>]
-Name <string>
[-ItemType <string>]
[-Value <Object>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[<CommonParameters>]
參數
以下是此cmdlet中使用的參數:
-path
-Path
參數用於指定新檔或檔夾的位置的路徑,接受通配符。
-ItemType-ItemType
參數指定新專案的指定提供者的類型。
如果用戶的位置在檔系統驅動器中,則允許使用這五個值(File,SymbolLink,Directory,Junction,HardLink)。
如果位置在認證驅動器中,則可以指定以下值:證書提供者,證書,商店,商店位置。
-Name
此參數指定新檔或檔夾的名稱。
-Value
此參數用於表示新專案的值。
-Force
此參數強制此cmdlet創建覆蓋現有只讀專案的專案。
-WhatIf
此參數描述了如果執行cmdlet將會發生的情況,該cmdlet不執行。
-Confirm
此參數在執行cmdlet之前提示進行確認。
示例
示例1: 在當前工作目錄中創建一個檔
PS E:\> cd .\xntutor\powershell\
PS E:\xntutor\powershell> new-item mynewfile.txt
目錄: E:\xntutor\powershell
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/1/30 9:03 0 mynewfile.txt
PS E:\xntutor\powershell>
在此示例中,該命令在當前工作目錄中創建一個文本檔(mynewfile.txt)。
示例2: 創建一個檔並將內容添加到該檔
本示例中的命令創建一個文本檔,然後在cmdlet中帶有-Value
參數。
示例3: 創建目錄
PS E:\xntutor\powershell> new-item -path "E:\xntutor\" -name "newdir" -itemtype "directory"
目錄: E:\xntutor
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/1/30 9:06 newdir
本示例中的命令在給定的驅動器中創建目錄。 在此命令中,-ItemType
參數表示目錄中的新專案。
示例4: 創建多個檔
PS E:\xntutor\powershell> new-item -path "E:\xntutor\textfile1.txt", "E:\xntutor\file2.txt"
目錄: E:\xntutor
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/1/30 9:08 0 textfile1.txt
-a---- 2020/1/30 9:08 0 file2.txt
在此示例中,該命令在兩個不同的目錄中創建新檔。 如果要創建多個專案,則-Path
參數接受多個字串。