共用方式為


直接操作專案

您在 PowerShell 磁碟驅動器中看到的專案,例如檔案和資料夾或登錄機碼,稱為 PowerShell 中的專案 。 使用它們的 Cmdlet 名稱中有名詞 Item

命令的 Get-Command -Noun Item 輸出會顯示有九個 PowerShell 專案 Cmdlet。

Get-Command -Noun Item
CommandType     Name                            Definition
-----------     ----                            ----------
Cmdlet          Clear-Item                      Clear-Item [-Path] <String[]...
Cmdlet          Copy-Item                       Copy-Item [-Path] <String[]>...
Cmdlet          Get-Item                        Get-Item [-Path] <String[]> ...
Cmdlet          Invoke-Item                     Invoke-Item [-Path] <String[...
Cmdlet          Move-Item                       Move-Item [-Path] <String[]>...
Cmdlet          New-Item                        New-Item [-Path] <String[]> ...
Cmdlet          Remove-Item                     Remove-Item [-Path] <String[...
Cmdlet          Rename-Item                     Rename-Item [-Path] <String>...
Cmdlet          Set-Item                        Set-Item [-Path] <String[]> ...

建立新專案

若要在文件系統中建立新專案,請使用 New-Item Cmdlet。 包含路徑為 專案的Path參數,以及值為或directoryfile ItemType參數。

例如,若要在 C:\Temp 目錄中建立名為 New.Directory 的新目錄,請輸入:

New-Item -Path c:\temp\New.Directory -ItemType Directory
    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        2006-05-18  11:29 AM            New.Directory

若要建立檔案,請將 ItemType 參數的值變更為 file 例如,若要在目錄中建立名為 file1.txtNew.Directory 檔案,請輸入:

New-Item -Path C:\temp\New.Directory\file1.txt -ItemType file
    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp\New.Directory

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2006-05-18  11:44 AM          0 file1

您可以使用相同的技術來建立新的登錄機碼。 事實上,登錄機碼更容易建立,因為 Windows 登錄中唯一的項目類型是機碼。 (登錄項目是項目 屬性。)例如,若要在子機碼中建立名為 _TestCurrentVersion 密鑰,請輸入:

New-Item -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\_Test
   Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion

SKC  VC Name                           Property
---  -- ----                           --------
  0   0 _Test                          {}

輸入登錄路徑時,請務必在PowerShell磁碟驅動器名稱和 HKLM:HKCU:中包含冒號 (:)。 如果沒有冒號,PowerShell 就無法辨識路徑中的磁碟驅動器名稱。

為什麼登錄值不是專案

當您使用 Get-ChildItem Cmdlet 尋找登錄機碼中的專案時,您將永遠不會看到實際的登錄專案或其值。

例如,登錄機碼 HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run 通常包含數個登錄專案,代表系統啟動時執行的應用程式。

不過,當您使用 Get-ChildItem 來尋找索引鍵中的子專案時,您只會看到 OptionalComponents 索引鍵的子機碼:

Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Run
   Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
SKC  VC Name                           Property
---  -- ----                           --------
  3   0 OptionalComponents             {}

雖然將登錄專案視為專案會很方便,但您無法以確保其唯一的方式指定登錄項目的路徑。 路徑表示法不會區分名為 Run 的登錄子機碼和 [執行] 子機碼中的 [預設] 登錄專案。 此外,因為登錄專案名稱可以包含反斜杠字元 (\),如果登錄專案是專案,則您無法使用路徑表示法來區分名為 Windows\CurrentVersion\Run 的登錄專案與位於該路徑中的子機碼。

重新命名現有的專案

若要變更檔案或資料夾的名稱,請使用 Rename-Item Cmdlet。 下列命令會將 file1.txt 檔案名變更為 fileOne.txt

Rename-Item -Path C:\temp\New.Directory\file1.txt fileOne.txt

Cmdlet Rename-Item 可以變更檔案或資料夾的名稱,但無法移動專案。 下列命令失敗,因為它會嘗試將檔案從 New.Directory 目錄移至 Temp 目錄。

Rename-Item -Path C:\temp\New.Directory\fileOne.txt c:\temp\fileOne.txt
Rename-Item : can't rename because the target specified isn't a path.
At line:1 char:12
+ Rename-Item  <<<< -Path C:\temp\New.Directory\fileOne c:\temp\fileOne.txt

移動專案

若要移動檔案或資料夾,請使用 Move-Item Cmdlet。

例如,下列命令會將 New.Directory 目錄從 C:\temp 目錄移至磁碟驅動器的 C: 根目錄。 若要確認專案已移動,請包含 Cmdlet 的 Move-Item PassThru 參數。 如果沒有 PassThru,CmdletMove-Item 就不會顯示任何結果。

Move-Item -Path C:\temp\New.Directory -Destination C:\ -PassThru
    Directory: Microsoft.PowerShell.Core\FileSystem::C:\

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        2006-05-18  12:14 PM            New.Directory

複製專案

如果您熟悉其他殼層中的複製作業,您可能會發現PowerShell中的 Cmdlet 行為 Copy-Item 異常。 當您將專案從某個位置複製到另一個位置時, Copy-Item 預設不會複製其內容。

例如,如果您從 C: 磁碟驅動器C:\temp將目錄複製到New.Directory目錄,命令會成功,但不會複製 New.Directory 目錄中的檔案。

Copy-Item -Path C:\New.Directory -Destination C:\temp

如果您顯示 的內容 C:\temp\New.Directory,您會發現它不包含任何檔案:

PS> Get-ChildItem -Path C:\temp\New.Directory
PS>

為什麼 Cmdlet 不會 Copy-Item 將內容複製到新位置?

Cmdlet Copy-Item 是設計成一般;它不只是用來複製檔案和資料夾。 此外,即使複製檔案和資料夾,您可能只想複製容器,而不是其中的專案。

若要複製資料夾的所有內容,請在 命令中包含 Cmdlet 的 Copy-Item Recurse 參數。 如果您已經複製目錄而沒有其內容,請新增 Force 參數,這可讓您覆寫空的資料夾。

Copy-Item -Path C:\New.Directory -Destination C:\temp -Recurse -Force -Passthru
    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        2006-05-18   1:53 PM            New.Directory

    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp\New.Directory

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2006-05-18  11:44 AM          0 file1

刪除專案

若要刪除檔案和資料夾,請使用 Remove-Item Cmdlet。 PowerShell Cmdlet,例如 Remove-Item,在輸入其命令時,可能會進行重大、不可復原的變更,通常會提示您進行確認。 例如,如果您嘗試移除 New.Directory 資料夾,系統會提示您確認命令,因為資料夾包含檔案:

Remove-Item C:\temp\New.Directory
Confirm
The item at C:\temp\New.Directory has children and the -recurse parameter was not
specified. If you continue, all children will be removed with the item. Are you
 sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):

因為 Yes 是預設回應,若要刪除資料夾及其檔案,請按 Enter 鍵。 若要移除資料夾而不需確認,請使用 Recurse 參數。

Remove-Item C:\temp\New.Directory -Recurse

執行專案

PowerShell 會 Invoke-Item 使用 Cmdlet 來執行檔案或資料夾的預設動作。 這個預設動作是由登錄中的默認應用程式處理程式所決定;效果與按兩下 檔案總管中的專案相同。

例如,假設您執行下列命令:

Invoke-Item C:\WINDOWS

位於 的 C:\Windows [總管] 視窗隨即出現,就像您按兩下 C:\Windows 資料夾一樣。

如果您在 Windows Vista 之前叫用系統上的 Boot.ini 檔案:

Invoke-Item C:\boot.ini

.ini如果文件類型與 記事本 相關聯,檔案會在 boot.ini 記事本 中開啟。