# powershell move files based on csv contents

**URL:** <https://forums.powershell.org/t/powershell-move-files-based-on-csv-contents/2741>\
**Category:** PowerShell Help\
**Created:** [June 23, 2014, 7:35pm UTC](https://forums.powershell.org/t/powershell-move-files-based-on-csv-contents/2741 "2014-06-23T19:35:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard-deans](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@richard-deans](https://forums.powershell.org/u/richard-deans)\
**Post date:** [June 23, 2014, 7:35pm UTC](https://forums.powershell.org/t/powershell-move-files-based-on-csv-contents/2741/1 "2014-06-23T19:35:14Z")

</div>

Hi All and thanks for helping!

I have produced a set of PDF files which are all saved in

L:\ClientSort\

Each of these files has a naming convention in the following way

XXX\_A123456\_YYY.PDF  
XXX\_300123123\_YYY.PDF

(the xxx stands for the brand)  
the yyy stands for account type)

What I need to do is move these PDF’s into one of two directories based on criteria within a CSV, excel or text file.

I will perform the logic of where the files should be sent within this file and within column D provide the file location to which I want to move the PDF.

Please see the attached spreadsheet to further clarify this point.

Basically… If the PDF naming convention contains the account number in column A, I need to move the file to the location specified in column D.

I have looked at as many different solutions as possible and the closed I could find was  
[Link to poss solution](http://stackoverflow.com/questions/23356289/powershell-script-to-move-files-based-on-a-source-list-txt)

Anyone have any ideas how this may be achieved?

Regards

Richard

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [June 23, 2014, 8:37pm UTC](https://forums.powershell.org/t/powershell-move-files-based-on-csv-contents/2741/2 "2014-06-23T20:37:40Z")

</div>

Something along these lines should work:

```
$moveTargets = @{}

$csvFile = '.\ClientDataSort.csv'
$sourceFolder = 'L:\ClientSort'

foreach ($record in Import-Csv -Path $csvFile)
{
    $moveTargets[$record.AccountRef] = $record.FileSortLocation
}

Get-ChildItem -Path $sourceFolder |
ForEach-Object {
    $pdfFile = $_
    if ($pdfFile.Name -match '^[^_]*_([^_]*)_[^_]*\.pdf$')
    {
        $accountRef = $matches[1]
        $target = $moveTargets[$accountRef]

        if ($target)
        {
            Move-Item -Path $pdfFile.FullName -Destination $target
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![richard-deans](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@richard-deans](https://forums.powershell.org/u/richard-deans)\
**Post date:** [June 23, 2014, 8:48pm UTC](https://forums.powershell.org/t/powershell-move-files-based-on-csv-contents/2741/3 "2014-06-23T20:48:05Z")

</div>

Thank you so much, I am going to have a try with this… I am starting out with powershell and it’s coding convention is baffling me so far. I am sure it will get easier!

I had used some of this coding in my build so far, reassuring it’s on the right path!

I’ll let you know how I get on…

Regards

Richard

---

<div class="post-metadata">

**Author:** ![richard-deans](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@richard-deans](https://forums.powershell.org/u/richard-deans)\
**Post date:** [June 23, 2014, 9:40pm UTC](https://forums.powershell.org/t/powershell-move-files-based-on-csv-contents/2741/4 "2014-06-23T21:40:53Z")

</div>

Spot on… Thanks so much!

---

<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:47pm UTC](https://forums.powershell.org/t/powershell-move-files-based-on-csv-contents/2741/5 "2024-05-16T20:47:52Z")

</div>

Closed on May 16, 2024, 8:47pm UTC


