How can I exclude multiple folders using Get-ChildItem -exclude?

前端 未结 12 922
星月不相逢
星月不相逢 2020-11-27 16:19

I need to generate a configuration file for our Pro/Engineer CAD system. I need a recursive list of the folders from a particular drive on our server. However I need to EXCL

12条回答
  •  醉梦人生
    2020-11-27 17:12

    The exclusion pattern should be case-insensitive, so you shouldn't have to specify every case for the exclusion.

    That said, the -Exclude parameter accepts an array of strings, so as long as you define $archive as such, you should be set.

    $archive = ("*archive*","*Archive*","*ARCHIVE*");

    You also should drop the trailing asterisk from $folder - since you're specifying -recurse, you should only need to give the top-level folder.

    $folder = "T:\Drawings\Design\"

    Fully revised script. This also changes how you detect whether you've found a directory, and skips the Foreach-Object because you can just pull the property directly & dump it all to the file.

    $folder = "T:\Drawings\Design\";
    $raw_txt = "T:\Design Projects\Design_Admin\PowerShell\raw.txt";
    $search_pro = "T:\Design Projects\Design_Admin\PowerShell\search.pro";
    $archive = ("*archive*","*Archive*","*ARCHIVE*");
    
    Get-ChildItem -Path $folder -Exclude $archive -Recurse  | where {$_.PSIsContainer}  | select-Object -expandproperty FullName |out-file $search_pro 
    

提交回复
热议问题