Hi all, I am a newbie in Powershell and tried my luck the last couple of days but haven’t really managed to get a script sorted. I know how to search for files recursively but don’t really know how to move them to their respective folder . Here is my issue . Root Folder c:\temp has multiple empty subfolders Called Test1 Test2 Test3 but also has other folders that contain files Test1file Test2file Test3file What I want to achieve is to look into all subfolders that contain files and move them to their respective folders Test1file->test1folder and then delete the empty subfolders .

Any help would be greatly appreciated .

9 Spice ups

I guess you have to compare names or something for powershell to know where where you want it to go.

What have you tried so far? Feel free to share your code and we’ll look over it

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

Welcome to the wonderful world of PowerShell and to the Spiceworks forum. This forum provides advice and guidance, but we do not operate a script writing service. The approach is that we can help you but we expect you to be doing some work too!

WIth respect to your query, you ue the Move-Item cmdlet to move the file. What you need to do is to first collect all the files then inside a foreach block, examine each file and move it as needed.

An outline might look like this:

# Get files
$FIles = Get-Childitem -Path <starting folder> -Recurse -File

# iterate
Foreach ($File in $Files ){
# examine the file name
If ($File.Fullname ....)  {
  $targetfolder = ...
  Move-Item $File.PSPath ...
}

You need to add the detail of how to test for a file to be moved and to work out the target folder.

3 Spice ups

Good answer on that one and nice example for them too!

What is going to happen when you have filename collisions?

Hi everyone ,

Thank you for the fast responses and all the help and sorry for my late response , I have been very busy over the last couple of days. I managed to achieve the desired outcome by using what you guys suggested and testing with the following Script which solved my problem.

# Script moves files from sub-folders (recursively) to the root folder and then deletes the empty folders
# Root of the client folders
$rootPath="E:\TestFolder\Testsubfolder\Subfolder"   
#
# If you want to exclude a folder from being processed
$ExcludeFolder = "E:\TestFolder\Testsubfolder\Subfolder\Foldertoexclude"

# Lists all the client folders in the root
$clientFolders = get-childitem -path $rootPath -Directory | Where-Object { $_.Fullname -ne $ExcludeFolder}

#Loop through each of the client folders
foreach ($clientFolder in $clientFolders) {
   
  # Loop through the subfolders and move the files to the root 
   Get-ChildItem $clientFolder.Fullname -Recurse -File | move-Item -Destination $clientFolder.Fullname  -Force -confirm:$false # -WhatIf
  
  # Remove the empty folders
  Get-ChildItem -Path $clientFolder.Fullname -Recurse -Directory  |Remove-Item -recurse -Force -confirm:$false # -WhatIf  -Force

}

#Get-ChildItem -Path $clientFolder.Fullname -Recurse -Directory |  Where-Object { -not $_.PSIsContainer } | Remove-Item -whatif