Unable to exclude directory using Get-ChildItem -Exclude parameter in Powershell

前端 未结 3 1819
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 15:53

I am using Powershell v 2.0. and copying files and directories from one location to another. I am using a string[] to filter out file types and also need to filter out a dir

相关标签:
3条回答
  • 2020-12-09 16:32
    Get-ChildItem -Path $SourcePath -File -Recurse | 
    Where-Object { !($_.FullName).StartsWith($DestinationPath) } 
    
    0 讨论(0)
  • 2020-12-09 16:34

    The -Exclude parameter is pretty broken. I would recommend you to filter directories that you don't want using Where-Object (?{}). For instance:

    $exclude = @('*.cs', '*.csproj', '*.pdb')
    $items = Get-ChildItem $parentPath -Recurse -Exclude $exclude | ?{ $_.fullname -notmatch "\\obj\\?" }
    

    P.S.: Word of warning – don't even think about using -Exclude on Copy-Item itself.

    0 讨论(0)
  • 2020-12-09 16:44

    I use this to list files under a root but not include the directories

    $files = gci 'C:\' -Recurse  | Where-Object{!($_.PSIsContainer)}
    
    0 讨论(0)
提交回复
热议问题