troubleshooting Question

Powershell - Create a folder from a file name in a CSV file, then move that file into that desination

Avatar of Richard Smith
Richard Smith asked on
10 Comments1 Solution85 ViewsLast Modified:
I have 6000 PST files that need to be moved from a source location to a server share.  Where my PowerShell is good enough to achieve this, if I had to move all the files in a given location, its not strong enough to figure out a way to do this when the PST files requiring the move are only 6000 out of 20000 in a complex folder structure.

I have a CSV file containing all the file paths of the PST files needing to be moved, and a single location to move them to, and again I would be able to script this, however, the requirement for my client is that the destination folder needs to be the name of the file that's moving.

So, I need help with something like this.

Source CSV file lists files as sample below.

Destination result would need to be


I would like to be able to leverage PowerShell to query my CSV file with the 6000 files listed within it (using a column header or not, which ever is easier), have it create a new folder per file, and move the file from the source location specified in the CSV to the central Server Share.

So far I have come up with the following code, but it can only manage to do all items in a given location.

Get-ChildItem -File |  # Get files
 Group-Object { $_.Name -replace '_.*' } |  # Group by part before first underscore
  ForEach-Object {
    # Create directory
    $dir = New-Item -ItemType directory -Path \\Server\Share\ -Name $_.Name
    # Move files there
    $_.Group | Move-Item -Destination $dir

I can't seem  to figure out how to replace "Get-ChildItem -File" with a import-CSV (for example).

Any help or pointers are fully appreciated.

Michael B. Smith
Managing Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros