[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PowerShell script to move files with duplicate names

Posted on 2014-02-25
2
Medium Priority
?
1,697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
Jason Ryberg earned 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

650 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