Script that copies files from multiple subfolders to a parent folder

Kaplan, Andrew H 226 Reputation points
2025-10-30T12:43:43.2266667+00:00

Hello.

I have a series of subfolders under a single parent folder. Each subfolder has a single file within it. I need to move the file in each subfolder to the parent and then remove the empty subfolder. Currently, I am doing this interactively, but I would like to automate the process. Is there a way in Powershell or batch file to accomplish this?

Windows for business | Windows Server | Performance | Application technologies and compatibility
0 comments No comments

Answer accepted by question author
Henry Mai 8,225 Reputation points Independent Advisor
2025-10-30T14:03:00.9466667+00:00

Dear Kaplan, Andrew H, I’m Henry.

I ran this script on my machine and it worked. You can try the following command in PowerShell.

$ParentFolder = "D:\Test"

Get-ChildItem -Path $ParentFolder -Directory | ForEach-Object {

$File = Get-ChildItem -Path $_.FullName -File

if ($File) {

    Move-Item -Path $File.FullName -Destination $ParentFolder -Force

    Remove-Item -Path $_.FullName -Force -Recurse

  }
}

Before, the 3 files were stored across 3 different foldersUser's image

After running the command:User's imageUser's image

You can replace your parent folder in script.

I hope you’ll give my recommendation a try and let me know how it goes and if this answer helps, feel free to hit “Accept Answer” so others can benefit too

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Oldest

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.