1
votes

I need to moves all the files from a folder to another but not the sub-folders or anything inside it. I have no idea of the name or the extension of the files I'll need to move (and some of them will not have any) so I can't use -Include. Here is an exemple of the structure of my folder:

  • Folder1
    • File1
    • File2
    • Folder2
      • File3
      • File4
  • Folder3

I want to move File1 and File2 in Folder3 without moving File3 and File4.

So far, I have something that looks like this:

$SouceFolder = "c:\Folder1\*"
$DestinationFolder = "c:\Folder3"

Move-Item -Path $SouceFolder -Destination $DestinationFolder

But it's copying the whole folder structure, with all the files.

Thanks.

Solution:

$SouceFolder = "c:\Folder1"
$DestinationFolder = "c:\Folder3"

Robocopy $SouceFolder $DestinationFolder /mov

Note: I removed the \* from the folder1.

4

4 Answers

2
votes

Did you try using -Recurse option like this?

Get-ChildItem -Path "c:\Folder1\*.*" -Recurse | Move-Item -Destination "c:\Folder3\"

There is better alternative Robocopy for copying files. With robocopy command you have better control on what is being copied with error codes if it fails.

in your case it would be just a simple code like this.

$SouceFolder = "c:\Folder1"
$DestinationFolder = "c:\Folder3"
Robocopy $SouceFolder $DestinationFolder

see the robocopy official documentation for more option

1
votes

This should work.

Note I'm not appending \* to the SourceFolder

$SourceFolder      = "c:\Folder1"
$DestinationFolder = "c:\Folder3"

# create the target folder if it does not already exist
if (!(Test-Path -Path $DestinationFolder -PathType Container)) {
    $null = New-Item -Path $DestinationFolder -ItemType Directory
}
Get-ChildItem -Path $SourceFolder -File | Move-Item -Destination $DestinationFolder
0
votes

Try this:

Get-Childitem "FilePath" -Recurse | Where-Object {!($_.PSIscontainer)} | Move-Item -Path $_.Fullname -Destination "Destination"