Need help using a CSV file to copy files to new subfolders using Powershell

I have a CSV file with headers FilePath and FileDestination. I can load it in an array with import-csv.

What I need to do is copy each file from FilePath (eg G:\99999\0001\CientWill1.PDF) to the FileDestination (e.g G:\99999\00001\Trusts and Wills\ClientWill1.PDF). Creating the subfolders as needed.

Something with Copy-Item / Foreach-object using the headers should work, but I've tried a ton of combinations, and had no success. Can anyone give me a bit of help getting this going?

Thanks,
sfaulstichAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, something like should work for you.  You could use -WhatIf if you needed to.

Regards,

Rob.

import-csv .\FileToCopy.txt | 
ForEach {
    if ((Test-Path -Path $_.FileDestination) -eq $false) {
        New-Item -ItemType Directory -Force -Path $_.FileDestination
    }
    Copy-Item -Path $_.FileName -Destination $_.FileDestination -Recurse -Force
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sfaulstichAuthor Commented:
Rob,

You are amazing. Thank you so much for the fast reply.
0
RobSampsonCommented:
Thanks!  If it works for you, please don't forget to close the question.  Otherwise, let me know if you need anything further, or any of it explained.

Rob.
0
sfaulstichAuthor Commented:
Rob gave me a fast answer after I was stumped. I very much appreciate the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.