Trying to index and search for a file within 8K files and 2K folders..
Is there a simple Powershell script that can move all files from folders and/or subfolders into one main folder?
don't need to delete the empty folders but would help.
The fourth example under help -Examples Move-Item
is close to what you need. To move all files under the source
directory to the dest
directory you can do this:
Get-ChildItem -Path source -Recurse -File | Move-Item -Destination dest
If you want to clear out the empty directories afterwards, you can use a similar command:
Get-ChildItem -Path source -Recurse -Directory | Remove-Item
I know this post is a bit old, but I came across a simmilar issue, but this doesn't cover the case when I need to maintain all fsubolder structure, so this is my solution maintaining the subfolder structure
$sourcePath = "C:\FolderLocation"
$destPath = "C:\NewFolderLocation"
Write-Host "Moving all files in '$($sourcePath)' to '$($destPath)'"
$fileList = @(Get-ChildItem -Path "$($sourcePath)" -File -Recurse)
$directoryList = @(Get-ChildItem -Path "$($sourcePath)" -Directory -Recurse)
ForEach($directory in $directoryList){
$directories = New-Item ($directory.FullName).Replace("$($sourcePath)",$destPath) -ItemType Directory -ea SilentlyContinue | Out-Null
}
Write-Host "Creating Directories"
ForEach($file in $fileList){
try {
Move-Item -Path $file.FullName -Destination ((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)) -Force -ErrorAction Stop
}
catch{
Write-Warning "Unable to move '$($file.FullName)' to '$(((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)))': $($_)"
return
}
}
Write-Host "Deleting folder '$($sourcePath)'"
Remove-Item -Path "$($sourcePath)" -Recurse -Force -ErrorAction Stop
Get-ChildItem -Path ./ -Recurse -File | Move-Item -Destination ./ ; Get-ChildItem -Path ./ -Recurse -Directory | Remove-Item;
– Vael Victus