Script that copies files from multiple subfolders to a parent folder

Kaplan, Andrew H 166 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
{count} vote

Answer accepted by question author
  1. Henry Mai 7,010 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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.