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

Welcome Forums General PowerShell Q&A 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

 
Keymaster
4 months ago.

  • Author
    Posts
  • #102724

    Participant
    Points: 0
    Rank: Member

    Hi,

    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.

    Thanks.

  • #102725

    Keymaster
    Points: 1
    Rank: Member

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

  • #102727

    Keymaster
    Points: 1
    Rank: Member

    Or possibly you meant...

    -notlike "*$excludefolder*"

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

The topic ‘How to exclude a sub folder and its contents using get-childitem’ is closed to new replies.