由 Windows PowerShell 磁碟機所公開的元素在 Windows PowerShell 中統稱為「項目」(Item),例如檔案系統磁碟機上的檔案和資料夾,以及 Windows PowerShell 登錄磁碟機上的登錄機碼。用於處理項目的 Cmdlet 其名稱中都有名詞 Item。
Get-Command -Noun Item 命令的輸出顯示了 Windows PowerShell 總共有九個項目 (Item) Cmdlet。
PS> 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)
若要在檔案系統中建立新項目,請使用 New-Item Cmdlet。然後將 Path 參數指定為項目的路徑,並將 ItemType 參數指定為 "file" 或 "directory" 值。
例如,若要在 C:\Temp 目錄中建立名稱為 "New.Directory" 的新目錄,請輸入:
PS> New-Item -Path c:\temp\New.Directory -ItemType Directory Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\temp Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2006-05-18 11:29 AM New.Directory
若要建立檔案,請將 ItemType 參數的值改為 "file"。例如,若要在 New.Directory 目錄中建立 "file1.txt" 檔案,請輸入:
PS> New-Item -Path C:\temp\New.Directory\file1.txt -ItemType file Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\temp\New.Directory Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2006-05-18 11:44 AM 0 file1
運用上述技巧也可以建立新登錄機碼。事實上,建立登錄機碼的做法更簡單,因為機碼是 Windows 登錄中唯一的項目類型 (登錄項目是項目「屬性」(Property))。例如,若要在 CurrentVersion 子機碼中建立 "_Test" 機碼,請輸入:
PS> New-Item -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\_Test Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Micros oft\Windows\CurrentVersion SKC VC Name Property --- -- ---- -------- 0 0 _Test {}
輸入登錄路徑時,Windows PowerShell 磁碟機名稱務必加上冒號 (:),例如 HKLM: 和 HKCU:。如果沒有冒號,Windows PowerShell 將無法辨識路徑中的磁碟機名稱。
登錄值為何不是項目
當您使用 Get-ChildItem Cmdlet 列出登錄機碼中的項目時,絕對看不到實際登錄項目或是項目值。
例如,HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run 登錄機碼通常包含數個登錄項目,代表系統啟動期間執行的應用程式。
但是,當您使用 Get-ChildItem 尋找機碼中的子項目時,就只會看到該機碼的 OptionalComponents 子機碼:
PS> Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Run Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Micros oft\Windows\CurrentVersion\Run SKC VC Name Property --- -- ---- -------- 3 0 OptionalComponents {}
將登錄項目視為項目固然方便,卻無法保證指定的路徑專指特定登錄項目。路徑標記法不能分辨 Run 登錄子機碼及 Run 子機碼中的 (Default) 登錄項目。此外,由於登錄項目名稱中可能包含反斜線字元 (\),因而若將登錄項目視為項目,使用路徑標記法即無法分辨 Windows\CurrentVersion\Run 登錄項目與位於該路徑上的子機碼。
重新命名現有的項目 (Rename-Item)
若要變更檔案或資料夾的名稱,請使用 Rename-Item Cmdlet。下列命令將 file1.txt 檔案的名稱變更為 fileOne.txt。
PS> Rename-Item -Path C:\temp\New.Directory\file1.txt fileOne.txt
Rename-Item Cmdlet 只能變更檔案或資料夾的名稱,無法移動項目。下列命令試圖將檔案從 New.Directory 目錄移到 Temp 目錄,因而發生失敗。
PS> Rename-Item -Path C:\temp\New.Directory\fileOne.txt c:\temp\fileOne.txt Rename-Item : Cannot rename because the target specified is not a path. At line:1 char:12 + Rename-Item <<<< -Path C:\temp\New.Directory\fileOne c:\temp\fileOne.txt
移動項目 (Move-Item)
若要移動檔案或資料夾,請使用 Move-Item Cmdlet。
例如,下列命令將 C:\temp 目錄中的 New.Directory 目錄移到 C: 磁碟機的根目錄。使用 Move-Item Cmdlet 時,加上 PassThru 參數即可確認是否已移動項目。如果沒有加上 Passthru,Move-Item Cmdlet 將不會顯示任何結果。
PS> Move-Item -Path C:\temp\New.Directory -Destination C:\ -PassThru Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2006-05-18 12:14 PM New.Directory
複製項目 (Copy-Item)
如果您已熟悉其他殼層的複製作業,應會發現 Windows PowerShell 中的 Copy-Item Cmdlet 行為不同於以往。當項目複製到其他位置時,Copy-Item 預設並未複製其內容。
例如,如果您將 C: 磁碟機上的 New.Directory 目錄複製到 C:\temp 目錄,命令雖已成功卻並未複製 New.Directory 目錄中的檔案。
PS> Copy-Item -Path C:\New.Directory -Destination C:\temp
當您顯示 C:\temp\New.Directory 的內容時,會發現裡面沒有任何檔案:
PS> Get-ChildItem -Path C:\temp\New.Directory PS>
Copy-Item Cmdlet 為何不將內容複製到新位置呢?
Copy-Item Cmdlet 的設計用途廣泛,不單只是複製檔案和資料夾而已。此外,在複製檔案和資料夾時,您可能只想複製容器而不複製其中的項目。
若要複製資料夾的全部內容,輸入命令時請加上 Copy-Item Cmdlet 的 Recurse 參數。如果您已複製空無一物的目錄,則可加上 Force 參數以覆蓋空資料夾。
PS> Copy-Item -Path C:\New.Directory -Destination C:\temp -Recurse -Force -Passthru Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\temp Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2006-05-18 1:53 PM New.Directory Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\temp\New.Directory Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2006-05-18 11:44 AM 0 file1
刪除項目 (Remove-Item)
若要刪除檔案和資料夾,請使用 Remove-Item Cmdlet。每當您所輸入的命令中包含 Remove-Item 這類會造成無法還原之重大變更的 Windows PowerShell Cmdlet 時,通常會出現確認提示。例如,若您嘗試移除 New.Directory 資料夾,就會提示您確認是否執行命令,因為該資料夾內有檔案:
PS> Remove-Item C:\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 參數。
PS> Remove-Item C:\temp\New.Directory -Recurse
執行項目 (Invoke-Item)
Windows PowerShell 使用 Invoke-Item Cmdlet 來執行檔案或資料夾的預設動作。此預設動作是由登錄中的預設應用程式控制碼所決定;效果等同於在 Windows 檔案總管中按兩下項目。
例如,若您執行下列命令:
PS> Invoke-Item C:\WINDOWS
這時便會出現 [檔案總管] 視窗並開啟 C:\Windows 位置,效果就跟按兩下 C:\Windows 資料夾一樣。
如果您在 Windows Vista 之前版本的系統上叫用 Boot.ini 檔案:
PS> Invoke-Item C:\boot.ini
若 .ini 檔案類型與記事本關聯,記事本就會開啟 boot.ini 檔案。