?
Solved

PowerShell script to move files with duplicate names

Posted on 2014-02-25
2
Medium Priority
?
1,600 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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)
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses

770 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