我对 Get-ChildItem 的 -Depth
标志有些困惑。以下示例很好地工作(在“C:\Program Files”下查找所有一级深度的文件和文件夹):
dir 'C:\Program Files\' -Depth 1
但是,如果我想要扩展它以仅查找*.txt类型的文件,则找不到如何实现此目标的方式(下面的命令将忽略-Depth 1并执行相当于对所有子文件夹进行递归搜索):
dir 'C:\Program Files\*.txt' -Depth 1
dir 'C:\Program Files\' -Include *.txt -Depth 1
dir 'C:\Program Files\*' -Include *.txt -Depth 1
如何使用-Depth
参数来获取特定深度和所需文件模式的Get-ChildItem
命令?
(Get-WMIObject win32_operatingsystem).name
的结果是Get-WMIObject: The term 'Get-WMIObject' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
。如果PS v7缺少WMI,那真的是一个致命问题。是否有模块可以重新添加WMI,以便在Windows上运行PS v7时使用,否则我就无法在我的工作或个人项目中使用它? :-( - YorSubsGet-CimInstance
)在PowerShell v3(于2012年9月发布)中取代了WMI cmdlets(例如Get-WmiObject
)。因此,应避免使用WMI cmdlets,尤其是因为所有未来的工作都将在PowerShell _Core_上进行,而它们已经不再拥有。有关更多信息,请参见此答案。 - mklement0