Solved

PowerShell script to move files with duplicate names

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
how to add IIS SMTP to handle application/Scanner relays into office 365.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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