Solved

PowerShell script to move files with duplicate names

Posted on 2014-02-25
2
1,488 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 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

Office 365 Training for IT Pros

Learn how to provision Office 365 tenants, synchronize your on-premise Active Directory, and implement Single Sign-On.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

739 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