powershell - 使用动态构造的排除模式数组,使用 Get-ChildItem -Exclude 过滤子文件夹

标签 powershell powershell-2.0 subdirectory get-childitem glob

我在“clients”目录中有以下文件夹

Arizona
California
Connecticut
Default
Delaware
Florida
Hawaii
Idaho
Iowa
Maine

我有一个数组,它包含以下项目:

Write-Host "Printing the array " 
$ExcludeArray

Output----
"Arizona",
"California"  

我正在尝试列出 C:\Users\santhu\Desktop\Clients 中的文件夹,并使用以下命令排除 $ExcludeArray 数组中的文件夹。

Get-Childitem C:\Users\santhu\Desktop\Clients -Force -Exclude $ExcludeArray

排除似乎没有按预期工作。我相信原因可能是 $ExcludeArray 中的项目不在同一行。如果有什么方法可以将它们转换为同一行并过滤文件夹,有人可以建议我吗? 仅供引用:$ExcludeArray 项不是静态的。我无法编写类似 $ExcludeArray = "Arizona","California" 的内容。我期待以下输出。

Mode                LastWriteTime     Length Name                                                                       
----                -------------     ------ ----                                                                                                                                                                                                       
d----         3/20/2017  12:15 PM            Connecticut                                                                
d----         3/20/2017  12:15 PM            Default                                                                    
d----         3/20/2017  12:15 PM            Delaware                                                                   
d----         3/20/2017  12:15 PM            Florida                                                                    
d----         3/20/2017  12:15 PM            Hawaii                                                                     
d----         3/20/2017  12:15 PM            Idaho                                                                      
d----         3/20/2017  12:15 PM            Iowa                                                                       
d----         3/20/2017  12:15 PM            Maine                                                                      
d----         3/20/2017  12:15 PM            Minnesota 

最佳答案

试试这个:

Get-Childitem C:\Users\santhu\Desktop\Clients\* -Force -Exclude $ExcludeArray

这里的排除和包含开关有点棘手。它适用于最后提到的路径。所以通配符应该完成你的工作。确保数组内没有任何双引号。

关于powershell - 使用动态构造的排除模式数组,使用 Get-ChildItem -Exclude 过滤子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43051864/

相关文章:

c++ - 如何区分控制台程序是在 Powershell 中打开还是在 Windows 终端中打开?

powershell - 有没有办法知道 Powershell 中的 cmdlet 版本以实现向后兼容?

powershell - 如何在非提升的 powershell 中运行 w32tm

r - 如何使用R或PowerShell从文本文件中提取数据?

powershell - PowerShell 2 CTP3 ISE 的自定义读取主机对话框

wordpress - 在 WordPress 中管理子目录的最佳做法是什么?

cakephp - 如何在子目录中安装 CakePHP 3

powershell - 多个Powershell进度条(嵌套?)

powershell - 我可以使参数集依赖于另一个参数的值吗?

java - 包名称与文件夹结构不同,但 Java 代码仍可编译