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


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.


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

Or possibly you meant…

-notlike “$excludefolder

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