在 Windows PowerShell 驱动器中导航和操作其上面的项类似于操作 Windows 物理磁盘驱动器上的文件和文件夹。 本节讨论如何使用 PowerShell 处理特定文件和文件夹操作任务。

可以通过使用 Get-ChildItem 直接获取某个文件夹中的所有项目。 添加可选的 Force 参数以显示隐藏项或系统项。 例如,此命令将显示 Windows PowerShell 驱动器 C(它与 Windows 物理驱动器 C 相同)的直观内容:

Get-ChildItem -Path C:\ -Force

执行上面示例代码结果如下所示:

PS C:\Users\maxsu> Get-ChildItem -Path C:\ -Force


    目录: C:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--hs-       2019/10/27      8:05                $360Section
d--hs-        2017/8/31     20:14                $Recycle.Bin
d-----        2019/6/21     13:38                360RecoveryEnv
d-rhs-        2020/1/27     22:57                360SANDBOX
d-----         2020/1/2     21:37                360安全浏览器下载
d-----        2019/9/16      8:34                AppData
d-----        2017/6/28      4:57                Apps
da----        2017/6/28     21:42                dell
d--hsl        2017/8/30     19:16                Documents and Settings
d-----        2019/1/10     20:44                downloads
d-----        2017/6/24      0:41                Drivers
d-----        2017/6/28      4:47                Intel
d-----       2019/12/13     22:31                kingsoft
d-----       2017/12/14     20:25                KuGou
d-----        2019/3/19     12:52                PerfLogs
d-r---        2019/9/22     15:56                Program Files
d-r---         2020/1/2     21:37                Program Files (x86)
d--h--        2020/2/15      0:05                ProgramData
d--h--         2019/8/4     23:02                Recovery
d--hs-        2020/2/11     18:57                System Volume Information
d-r---         2019/8/4     23:08                Users
d-----        2020/2/15      0:05                Windows
d-----        2019/9/16      9:09                迅雷下载
-arh--        2017/6/28     20:45          26397 dell.sdr
-arh--        2017/6/24      0:51          23059 fiod.sdr
-a-hs-        2020/2/15      8:17     6833668096 hiberfil.sys
-a----        2019/4/30      8:21            107 LibAntiPrtSc_ERROR.log
-a----       2019/11/27     17:11           1113 LibAntiPrtSc_INFORMATION.log
-a-hs-        2020/2/15      0:07     2550136832 pagefile.sys
-a-hs-        2020/2/15      0:07      268435456 swapfile.sys

该命令将仅列出直接包含的项,类似于使用 Cmd.exe 的 DIR 命令或 UNIX shell 中的 ls。 为了显示包含的项,你还需要指定 -Recurse 参数。 (这可能需要相当长的时间才能完成。)列出 C 驱动器上的所有内容:

Get-ChildItem -Path C:\ -Force -Recurse

Get-ChildItem 可以使用其 Path、Filter、Include 和 Exclude 参数筛选项,但那些通常只基于名称。 还可以通过使用 Where-Object 基于项的其他属性执行复杂的筛选。
下面的命令用于查找上次于 2005 年 10 月 1 日之后修改,并且不小于 1 兆字节,也不大于 10 兆字节的 Program Files 文件夹中的所有可执行文件:

Get-ChildItem -Path $env:ProgramFiles -Recurse -Include *.exe | Where-Object -FilterScript {($_.LastWriteTime -gt '2005-10-01') -and ($_.Length -ge 1mb) -and ($_.Length -le 10mb)}