0

I have a folder named "Folder-Test". In the "Folder-Test" folder there are 3 sub-folders: "A", "B-1", "C-1".

How do I use "Get-ChildItem" to get a list of folders containing "-" in the name ("B-1", "C-1"). Then, use "ForEach-Object" and "Copy-Item" to copy the folders containing "-" in the name ("B-1", "C-1") to another folder.

My old code:

$path = (gci "D:\Folder-All" -Filter "Folder-Test" -Recurse).FullName
mkdir "D:\Another-Folder"
Get-ChildItem $path -Directory |
ForEach-Object {if (($_.enumeratefiles() | measure).count -gt 0)
{Copy-Item $path "D:\Another-Folder" -Filter {PSIsContainer} -Recurse -Force}
               }

1 Answer 1

1

This gets all directories in "D:\Folder-All\Folder-Test" that contain a dash in the name and recursively copies them to "D:\Another-Folder".

Get-ChildItem -Path "D:\Folder-All\Folder-Test\*-*" -Directory | 
Copy-Item -Destination "D:\Another-Folder" -Recurse
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.