PowerShell Move (Move-Item) files to designated folders based on CSV file

Recently I had to move over 5000 documents to their respective folders.


There there two columns in a csv file. First column contains folder name and second column the filename. The files had to be moved to individual folders. There can be multiple documents per folder. You need to ensure that the folders have been created in the $dir location.

#PowerShell script to create folder

Set-Location “D:\Export\Files”

$Folders = Import-Csv D:\folders.csv -Header folders

 ForEach($Folder in $Folders) {

 New-Item $Folder.folders -type directory


 #Here is the PowerShell script for moving the files in required folder:

$text = Import-Csv “D:\InputFile.csv” 

$dir = Get-ChildItem D:\Export\Files

 foreach($item in $text ){

 foreach ($folder in $dir) {

#where Package is the name of the column in the csv file and FileName the name of #second column in the input csv file

 if($item.Package – eq $Folder.Name){

 $sourcefile = “D:\Export\” + $item.Filename 

$target = “D:\Export\Files\” + $folder.Name


Write-Host“Folder $item.FileName moved sucessfully to $target”-ForegroundColor Green





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s