How to exclude a sub folder and its contents using get-childitem

This topic contains 2 replies, has 2 voices, and was last updated by  Don Jones 1 month ago.

  • Author
  • #102724

    Dinesh Kumar


    I have a folder which contain both sub folders and files. I want to copy all the contents (Files,subfolders and its contents) excluding a specific subfolder and its contents. I tried the below one but it didn't work.

    $inputFolder = 'D:\dinesh\run'
    $excludefolder= 'dataset'
    $target = 'E:\kumar\run'
    Get-ChildItem -path $inputFolder -Recurse | Where {$_.FullName -notlike $excludefolder} | Copy-Item -Destination {Join-Path $target $_.FullName.Substring($inputFolder.length)}

    The above command is copying the $excludefolder contents as well but it should not.
    Can any one suggest a way to get it done.


  • #102725

    Don Jones

    You're comparing FullName, which is generally the entire folder path. Maybe you just meant Name?

  • #102727

    Don Jones

    Or possibly you meant...

    -notlike "*$excludefolder*"

    instead? Without wildcards like *, -notlike isn't really doing a wildcard match, it's the same as -ne.

You must be logged in to reply to this topic.