Solved

PowerShell script to move files with duplicate names

Posted on 2014-02-25
2
1,348 Views
Last Modified: 2014-03-05
I have a list of PST files on a network share containing users home folders. The list of files is in CSV format, and contains the fields "User" (contains usernames) and "filename" (full path and file name). I need to do the following with these PST's:

1. Create a folder in another specified location for holding the PST's for that user. For example, \\<server>\PST\<username>. I already have this part working.
2. Move the PST files into the folders for the appropriate users. The list of PST files contains user names corresponding to the owner of the file.
3. I will eventually import the PST's into the users mailboxes using PowerShell with a separate script. I am able to do this part as well.

My problem is with step 2. I need to move all the PST's into the proper users folder, but some have duplicate file names (i.e. Archive.pst) in separate sub-folders in their home folders.  I need to copy all into Exchange.

How can I automatically rename duplicate files when I move them into the users new folder? The name needs to retain the original file name, although text can be appended to the name if needed. It must also keep .PST as the suffix.


Thanks in advance for any suggestions.

Dan
0
Comment
Question by:drothbart
2 Comments
 
LVL 4

Accepted Solution

by:
Jason Ryberg earned 500 total points
ID: 39903665
Dan,

Sounds like the issue you're up against is breaking the filename out of the fullpath.  This script below should do that for you and copy it to the target location.

$CSVFile | % {

  $FullPath = $_.Filename

  #Need to pull just the filename from the full path.  This command splits the $FullPath string into an array based on the "\" character; it then grabs the last member of the array
  $FileName = $($FullPath.Split("\"))[-1]

  #Append the filename to the target path and test to see if the file already exists; if it does, add a unique ID
  $TargetFile = "\\<server>\PST\$($_.User)\$FileName"
  If (Test-Path $TargetFile) {
    $FileName = $FileName.Replace(".PST","-import.PST") #you can replace the second parameter of the replace method with whatever text you want to append instead of "-import"
    $TargetFile = "\\<server>\PST\$($_.User)\$FileName"
  }

  Copy-Item $FullPath $TargetFile

}

Open in new window


Jason
0
 
LVL 5

Author Closing Comment

by:drothbart
ID: 39907490
Excellent, well-commented answer. Thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now