# Move items with folder structure

**URL:** <https://forums.powershell.org/t/move-items-with-folder-structure/20323>\
**Category:** PowerShell Help\
**Created:** [September 1, 2022, 4:20pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323 "2022-09-01T16:20:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![vat2022](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vat2022](https://forums.powershell.org/u/vat2022)\
**Post date:** [September 1, 2022, 4:20pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/1 "2022-09-01T16:20:16Z")

</div>

I have a file storage server with documents are stored in the different locations. See file listing below. I need to move all of the items in the folder called **Archive** to another backup server and keep the folder structure. I tried Get-ChildItem, Move-item, Split-path but I can’t get it to work. Can someone please help me? Thank you in advance.

```auto
C:\Documents\TestData\E0001\E0001_0001\report.xls
C:\Documents\TestData\E0001\E0001_0001\Archive\bank_statement.docx
C:\Documents\TestData\E0001\E0001_0003\Info_gov.docx
C:\Documents\TestData\E0001\E0001_0003\Archive\Monthly Billing Report.xlsx
C:\Documents\TestData\E0001\E0001_0003\Archive\HomeDrive\server_backup.txt
C:\Documents\TestData\E0002\E0002_0001\Archive\9047_Payment_History.pdf
C:\Documents\TestData\E0002\E0002_0002\employee_contactlist.doc
C:\Documents\TestData\E0002\E0002_0002\Archive\working_document.pdf
C:\Documents\TestData\E0002\E0002_0003\Archive\New folder\9570_Pay_History.pdf
C:\Documents\TestData\E0003\Archive\Agreement Terms.pdf
C:\Documents\TestData\E0003\Archive\Bank Statement eStmt_2023-10-07.pdf

```

Here is what I tried. I just can’t get array03(which is the source path) and array04(which is destination) in a table format like SQL to loop through each for the move-item cmdlet. If this doesn’t work, I need a batch file move or copy command.

```auto
$filepath = 'C:\Documents\TestData\'

# create array01 to get all the files, including subfolders and only file type.
$array01 =@() 
$array01 = Get-Childitem $filepath -Name -Recurse -File 

# create array02 to match filename with word Archive
$array02 = @()
$array02 = $array01 -match 'Archive' 

# create array03 to (1)get the matched items and (2)write out full path for all matched items.
$array03 = @()
#$array02 | ForEach-object {Write-Output "C:\Documents\TestData\\$_"} <--replace this hard path to variable $filepath
$array03 = $array02 | ForEach-object {Write-Output "$filepath\$_"}

# for parent path folder
$array04 = @()
$array04 = $array03 | ForEach-object {split-path $_ -parent}

# either create a batch copy file or use Move-Item
$array05 = @()
$array05 = $array03 | ForEach-object {Write-Output "Copy ""$array03"" $_" } |Format-Table
$array05 = ForEach-object {Move-Item -path $array03 -destination $array04 }

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 1, 2022, 7:08pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/2 "2022-09-01T19:08:35Z")

</div>

No,  
Welcome to the forum. 👋🏽

With a little string acrobatics it is not that hard … 😉 😀

```auto
$SourceDir = 'C:\Documents\TestData'
$TargetDir = 'D:\Backup'

Get-ChildItem -Path $SourceDir -Directory -Recurse |
Where-Object -Property FullName -Match -Value '\\Archive(?!\\)' |
ForEach-Object {
    $SourceDirLength = $SourceDir.Length
    $TotalLength = $_.FullName.Length
    $RelativePath = $_.FullName.Substring($SourceDirLength, $TotalLength - $SourceDirLength)
    $TargetPath = Join-Path -Path $TargetDir -ChildPath $RelativePath
    Copy-Item -Path $_.FullName -Destination $TargetPath -Recurse 
}

```

That snippet will copy all `Archive` folders including their subfolders to the `$TargetDir`.

The regex pattern ` '\\Archive(?!\\)'` will match all paths with a folder with the name `Archive` in it but ignores their subfolders.

**Edit:**  
When you tested the code succesfully with test data you can of course change `Copy-Item` to `Move-Item`. 😉

---

<div class="post-metadata">

**Author:** ![vat2022](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vat2022](https://forums.powershell.org/u/vat2022)\
**Post date:** [September 1, 2022, 8:54pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/3 "2022-09-01T20:54:18Z")

</div>

Thank you for the warm welcome, Olaf!

The script works for me. I’m into my first week of learning PowerShell, so thank you so much! Much appreciated! You’re a genius!

Is there a way that I can get a report of all the files are copied or moved? Thank you.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 1, 2022, 9:26pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/4 "2022-09-01T21:26:09Z")

</div>

> [@vat2022](#):
>
> Is there a way that I can get a report of all the files are copied or moved?

You should ALWAYS read the help for the cmdlets you’re about to use COMPLETELY including the examples to learn how to use them.

Most cmdlets, which by default do not generate any output, can be forced to output something with the parameter `-PassThru`. This output can be piped to a TXT or CSV file.

> **[Move-Item (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item?view=powershell-7.2)**
>
> The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another location. The locations must be supported by the same provider. For example, it can move a file or subdirectory from one directory...

If that does not work you might try one of the common parameters supported by almost all cmdlets:

> **[about CommonParameters - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.2)**
>
> Describes the parameters that can be used with any cmdlet.

`-OutVariable` for example.

---

<div class="post-metadata">

**Author:** ![vat2022](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vat2022](https://forums.powershell.org/u/vat2022)\
**Post date:** [September 1, 2022, 10:07pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/5 "2022-09-01T22:07:59Z")

</div>

Thank you very much! Very helpful information.

I added **Start-Transcript** and **Stop-Transcript** to export to a report file.

I also added **-Verbose** and **-PassThru**. They are what I need.

---

<div class="post-metadata">

**Author:** ![vat2022](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vat2022](https://forums.powershell.org/u/vat2022)\
**Post date:** [September 14, 2022, 7:13pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/7 "2022-09-14T19:13:50Z")

</div>

I’m sorry. I figured it out now. Miss-typed a word.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 14, 2022, 8:46pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/8 "2022-09-14T20:46:49Z")

</div>

> [@vat2022](#):
>
> Miss-typed a word.

That happens to the best of us … 😉 😜 😃

---

<div class="post-metadata">

**Author:** ![vat2022](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vat2022](https://forums.powershell.org/u/vat2022)\
**Post date:** [September 14, 2022, 9:20pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/9 "2022-09-14T21:20:10Z")

</div>

🤣 I know right. kakakaa. I figured it out right after I asked you for help. OMG. lol

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 14, 2022, 9:37pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/10 "2022-09-14T21:37:01Z")

</div>

That happens quite often. I think it could be because you just wrapped your head around the issue explaining it to someone else (sometimes even in a foreign language) and then your brain is almost there but you do not realise it yet.

> [@vat2022](#):
>
> kakakaa

May I ask where you’re from?

---

<div class="post-metadata">

**Author:** ![vat2022](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vat2022](https://forums.powershell.org/u/vat2022)\
**Post date:** [September 14, 2022, 9:40pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/11 "2022-09-14T21:40:53Z")

</div>

Totally understand. I’m Vietnamese. SQL dev guy and learning PowerShell. How about you?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 14, 2022, 9:47pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/12 "2022-09-14T21:47:57Z")

</div>

I’m German. System engineer doing PowerShell since version 1.1.  
🧔‍♂️

Cheers.  
🙂 🤟🏽

---

<div class="post-metadata">

**Author:** ![vat2022](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vat2022](https://forums.powershell.org/u/vat2022)\
**Post date:** [September 15, 2022, 1:18am UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/13 "2022-09-15T01:18:39Z")

</div>

Cheers. 😀🕶 🍻 That sounds like a very long time ago. 🙂 You must love it alot.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 15, 2022, 6:40am UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/14 "2022-09-15T06:40:06Z")

</div>

It is. 15 Years.  
I do.  
🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/move-items-with-folder-structure/20323/15 "2024-05-16T20:16:36Z")

</div>


