当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 直接操作项目

作者:精品下载站 日期:2024-12-14 03:03:00 浏览:13 分类:玩电脑

直接操作项目


您在 PowerShell 驱动器中看到的元素(例如文件和文件夹或注册表项)在 PowerShell 中称为。用于使用这些项目的 cmdlet 名称中包含名词 Item

Get-Command -Noun Item 命令的输出显示有九个 PowerShell item 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 参数,以及值为 filedirectoryItemType 参数。

例如,要在 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。例如,要在 New.Directory 目录中创建名为 file1.txt 的文件,请键入:

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 注册表中唯一的项目类型是注册表项。 (注册表项是项属性。)例如,要在 CurrentVersion 子项中创建名为 _Test 的项,请键入:

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 的注册表子项和 Run 子项中的(默认) 注册表项。此外,由于注册表项名称可以包含反斜杠字符 (\),因此如果注册表项是项目,则无法使用路径表示法来区分名为 Windows\CurrentVersion\ 的注册表项从位于该路径中的子项运行

重命名现有项目

要更改文件或文件夹的名称,请使用 Rename-Item cmdlet。以下命令将 file1.txt 文件的名称更改为 fileOne.txt

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

Rename-Item cmdlet 可以更改文件或文件夹的名称,但无法移动项目。以下命令失败,因为它尝试将文件从 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: 驱动器的根目录。要验证项目是否已移动,请包含 Move-Item cmdlet 的 PassThru 参数。如果没有 PassThruMove-Item cmdlet 将不会显示任何结果。

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

复制项目

如果您熟悉其他 shell 中的复制操作,您可能会发现 PowerShell 中的 Copy-Item cmdlet 的行为不寻常。当您将一项从一个位置复制到另一个位置时,Copy-Item 默认情况下不会复制其内容。

例如,如果将 New.Directory 目录从 C: 盘复制到 C:\temp 目录,命令会成功,但 New.Directory 中的文件目录未复制。

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参数,该参数允许您覆盖空文件夹。

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 文件将在记事本中打开。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯