在 Windows PowerShell 驱动器中看到的元素(例如文件系统驱动器中的文件和文件夹)和 Windows PowerShell 注册表驱动器中的注册表项在 Windows PowerShell 中均称为项。用于处理这些项的 cmdlet 的名称中具有名词 Item。
Get-Command -Noun Item 命令的输出表明存在九个 Windows PowerShell 项 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 参数,以及值为“file”或“directory”的 ItemType 参数。
例如,若要在 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 注册表中只有项类型属于项,因此创建注册表项更加容易。(注册表条目是项属性。)例如,若要在 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。
例如,以下命令可以将 New.Directory 目录从 C:\temp 目录移动至 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)
如果您熟悉其他 shell 中的复制操作,则可能会发现 Windows PowerShell 中的 Copy-Item cmdlet 的行为有些不同。在将某个项从一个位置复制到另一位置时,默认情况下,Copy-Item 不会复制其内容。
例如,如果将 New.Directory 目录从 C: 驱动器复制到 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。Windows PowerShell cmdlet(例如 Remove-Item)可以进行无法撤消的重大更改,当您输入命令时,它通常会提示您进行确认。例如,如果尝试删除 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"):
由于是为默认响应,因此若要删除文件夹及其文件,请按 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 文件类型与 Notepad 相关联,则 boot.ini 文件将在 Notepad 中打开。