Windows PowerShell 使用名词项来表示在 Windows PowerShell 驱动器中找到的数据。当处理 Windows PowerShell FileSystem 提供程序时,项可以是文件、文件夹或 Windows PowerShell 驱动器。在大多数管理设置中,列出并处理这些项是一项重要的基本任务,因此我们应对这些任务进行详细的讨论。<o:p></o:p>
由于从特定位置获取项集合是非常常见的任务,因此 Get-ChildItem cmdlet 旨在返回容器(例如文件夹)中找到的所有项。
若要返回文件夹 C:\Windows 中直接包含的所有文件和文件夹,请键入:
PS> Get-ChildItem -Path C:\Windows<o:p></o:p> Directory:Microsoft.Windows PowerShell.Core\FileSystem::C:\Windows<o:p></o:p> Mode LastWriteTime Length Name<o:p></o:p> ---- ------------- ------ ----<o:p></o:p> -a--- 2006-05-16 <st1:timeminute10hour w:st="on">8:10 AM</st1:timeminute10hour> 0 0.log<o:p></o:p> -a--- 2005-11-29 <st1:timeminute16hour w:st="on"><st1:timeminute16hour w:st="on">3:16 PM</st1:timeminute16hour> 97</st1:timeminute16hour> acc1.txt<o:p></o:p> -a--- 2005-10-23 <st1:timeminute21hour w:st="on">11:21 PM</st1:timeminute21hour> 3848 actsetup.log<o:p></o:p> ...<o:p></o:p> |
<o:p> </o:p>
在 Cmd.exe 中输入 dir 命令或在 UNIX 命令外壳程序中输入 ls 命令时,所列出的内容与您所看到的内容相似。
可以使用 Get-ChildItem cmdlet 参数来执行非常复杂的列表操作。我们将看到以下几种情况。您可以通过键入以下命令来查看 Get-ChildItem cmdlet 的语法:
PS> Get-Command -Name Get-ChildItem -Syntax<o:p></o:p> |
<o:p> </o:p>
可以将这些参数进行混合和匹配以获得较高程度的自定义输出。
若要查看 Windows 文件夹内部的项及其子文件夹中包含的所有项,请使用 Get-ChildItem 的 Recurse 参数。列表操作将显示 Windows 文件夹中的所有内容及其子文件夹中的所有项。例如:
PS> Get-ChildItem -Path C:\WINDOWS -Recurse<o:p></o:p> <o:p> </o:p> Directory:Microsoft.Windows PowerShell.Core\FileSystem::C:\WINDOWS<o:p></o:p> Directory:Microsoft.Windows PowerShell.Core\FileSystem::C:\WINDOWS\AppPatch<o:p></o:p> Mode LastWriteTime Length Name<o:p></o:p> ---- ------------- ------ ----<o:p></o:p> -a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 1852416 AcGenral.dll<o:p></o:p> ...<o:p></o:p> |
<o:p> </o:p>
若要仅显示项名称,请使用 Get-Childitem 的 Name 参数:
PS> Get-ChildItem -Path C:\WINDOWS -Name<o:p></o:p> addins<o:p></o:p> AppPatch<o:p></o:p> assembly<o:p></o:p> ...<o:p></o:p> |
<o:p> </o:p>
通常,在 Windows 资源管理器或 Cmd.exe 中可见的项均不会显示在 Get-ChildItem 命令的输出中。若要显示隐藏项,请使用 Get-ChildItem 的 Force 参数。例如:
Get-ChildItem -Path C:\Windows -Force<o:p></o:p> |
<o:p> </o:p>
由于可强制覆盖 Get-ChildItem 命令的正常行为,因此此参数命名为 Force。Force 参数使用广泛,它可强制执行 cmdlet 非正常执行的操作,但该参数不执行危及系统安全的任何操作。
Get-ChildItem 命令在要列出的项路径中接受通配符。
由于通配符匹配是通过 Windows PowerShell 引擎处理的,因此接受通配符的所有 cmdlet 都可使用相同的表示法,并具有相同的匹配行为。Windows PowerShell 通配符表示法包括:
· 星号 (*) 与零或更多出现的字符匹配。<o:p></o:p>
· 问号 (?) 仅与一个字符匹配。<o:p></o:p>
· 左方括号 ([) 字符和右方括号 (]) 字符可括住一组要进行匹配的字符。 <o:p></o:p>
此处为如何应用通配符规范的一些示例。
若要查找 Windows 目录中具有后缀 .log,且基名称只有五个字符的所有文件,请输入以下命令:
PS> Get-ChildItem -Path C:\Windows\?????.log<o:p></o:p> Directory:Microsoft.Windows PowerShell.Core\FileSystem::C:\Windows<o:p></o:p> Mode LastWriteTime Length Name<o:p></o:p> ---- ------------- ------ ----<o:p></o:p> ...<o:p></o:p> -a--- 2006-05-11 <st1:timehour18minute w:st="on">6:31 PM</st1:timehour18minute> 204276 ocgen.log<o:p></o:p> -a--- 2006-05-11 6:31 PM 22365 ocmsn.log<o:p></o:p> ...<o:p></o:p> -a--- 2005-11-11 <st1:timehour4minute w:st="on">4:55 AM</st1:timehour4minute> 64 setup.log<o:p></o:p> -a--- 2005-12-15 <st1:timeminute24hour w:st="on">2:24 PM</st1:timeminute24hour> 17719 VxSDM.log<o:p></o:p> ...<o:p></o:p> |
<o:p> </o:p>
若要查找 Windows 目录中以字母 x 开头的所有文件,请键入:
Get-ChildItem -Path C:\Windows\x*<o:p></o:p> |
<o:p> </o:p>
若要查看其文件名以 x 或 z 开头的所有文件,请键入:
Get-ChildItem -Path C:\Windows\[xz]*<o:p></o:p> |
<o:p> </o:p>
通过使用 Get-ChildItem 的 Exclude 参数可以排除特定的项。即,在单语句中执行复杂的筛选操作。
例如,假设您要在 System32 文件夹中查找 Windows 时间服务 DLL,但您只记得 DLL 名称是以“W”开头并且其中有数字“32”。
使用类似表达式 w*32*.dll 即可找到符合该条件的所有 DLL,但该表达式还会返回其名称中包含“95”或“16”的与 Windows 95 和 16 位 Windows 兼容的 DLL。您可以使用模式为 *[9516]* 的 Exclude 参数省略名称中具有这些数字的所有文件:
PS> Get-ChildItem -Path C:\WINDOWS\System32\w*32*.dll -Exclude *[9516]*
<o:p> </o:p>
<o:p> </o:p>
Directory:Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\System32
<o:p> </o:p>
<o:p> </o:p>
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 174592 w32time.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 22016 w32topl.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 101888 win32spl.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 172032 wldap32.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 264192 wow32.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 82944 ws2_32.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 42496 wsnmp32.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 22528 wsock32.dll
-a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 18432 wtsapi32.dll
您可以在同一命令中使用 Get-ChildItem cmdlet 的多个参数。在混合使用参数之前,请确保您了解通配符匹配。例如,以下命令将不会返回任何结果:
PS> Get-ChildItem -Path C:\Windows\*.dll -Recurse -Exclude [a-y]*.dll<o:p></o:p> |
<o:p> </o:p>
即使 Windows 文件夹中包含两个以字母“z”开头 DLL,也不会返回任何结果。
不返回任何结果是由于我们将通配符指定为该路径的一部分。即使该命令是递归的,但 Get-ChildItem cmdlet 仍将这些项限制为 Windows 文件夹中其名称以“.dll”结尾的项。
若要为其名称与特定模式相匹配的文件指定递归搜索,请使用 -Include 参数。
PS> Get-ChildItem -Path C:\Windows -Include *.dll -Recurse -Exclude [a-y]*.dll<o:p></o:p> <o:p> </o:p> Directory:Microsoft.Windows PowerShell.Core\FileSystem::C:\Windows\System32\Setup<o:p></o:p> <o:p> </o:p> <o:p> </o:p> Mode LastWriteTime Length Name<o:p></o:p> ---- ------------- ------ ----<o:p></o:p> -a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 8261 zoneoc.dll<o:p></o:p> <o:p> </o:p> <o:p> </o:p> Directory:Microsoft.Windows PowerShell.Core\FileSystem::C:\Windows\System32<o:p></o:p> <o:p> </o:p> <o:p> </o:p> Mode LastWriteTime Length Name<o:p></o:p> ---- ------------- ------ ----<o:p></o:p> -a--- 2004-08-04 <st1:timehour8minute w:st="on">8:00 AM</st1:timehour8minute> 337920 zipfldr.dll<o:p></o:p> <o:p> </o:p> |