PST Find,Rename and Move

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

PST Find,Rename and Move

Post by dwight.brookland »

Hello,

I need help trying to figure out how to take PST files owned by a user on a network drive. It would need to

1) Locate PST Files (This is not hard) and add to array for that user
2) Copy PST files with new name to centralized location. PST Names should be the same as the user's root H: Drive folder and something to identify (numerical value) that each one is different.
3) Set permissions on the original PST File or Delete it straight out.

The issue is that MANY or MOST users in or org have not just 1 or 2 PST files but 5 to 10 or more. I have the powershell portion of identifying the pst files but how to copy and rename them to a central location by either the folder they sit in and/or numerical value identifier eludes me.

Thanks,
Dwight
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

I would also like to include the original file name in the new file name.

Example:
Original file:
\\Server\Home\BobbyB\Outlook\bobby.pst
\\Server\Home\BobbyB\Outlook\Archive.pst
\\Server\Home\BobbyB\TermUser\DaveA\Dave.pst
\\Server\Home\BobbyB\TermUser\DaveA\Archive.pst

New File:
\\Server\IT\PSTs\BobbyB_Outlook_bobby.pst
\\Server\IT\PSTs\BobbyB_Outlook_Archive.pst
\\Server\IT\PSTs\BobbyB_DaveA_Dave.pst
\\Server\IT\PSTs\BobbyB_DaveA_Archive.pst

This way it has the ability to continually identify who the file belongs to NOW, as well as what folder it was in so we can make some level of direction when setting them up for import into the user's mailbox in Exchange Online.
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

Update - How would you handle at that point assigning iterations if files are the same. Add numerical values to the end of the name with a replace value and add a n++ value to the end with .pst?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PST Find,Rename and Move

Post by jvierra »

Can you post your script. Are you getting errors? If so, please post the complete error message.
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

This is conceptualization not code yet. Looking for direction or examples of how to process or grab specific pieces of a unc path or file object.. Was just looking for ideas.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PST Find,Rename and Move

Post by jvierra »

I recommend that you write your script and then post a specific question.

You can check if a filename is I use with "Test-Path".

help test-path -Full

CHeck the examples to see how to use the command.
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

Here is the script
PSTRename_MoveV1.ps1
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

Code: Select all

<#	
	.NOTES
	===========================================================================
	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.153
	 Created on:   	8/15/2018 3:12 PM
	 Created by:   	
	 Organization: 	
	 Filename:     	
	===========================================================================
	.DESCRIPTION
		PST Migration Script.
#>
$searchClasses = "UNC Search", "File Search"
$c = 1
$searchClasses | Foreach {
	Write-Host $c.ToString() -NoNewLine
	Write-Host " --- " -NoNewLine
	Write-Host $_ -ForegroundColor Green
	$c++
}

[int]$searchClass = Read-Host "Select the search class"
$choice = $searchClasses[$searchClass - 1]
Write-Host $choice -ForegroundColor Yellow
## BUILD USER FOLDER LIST ##
If ($choice -eq "UNC Search")
{
	$userFilePath = Read-Host "Enter UNC Path to User Folders: (Example: \\Thermon.local\USSM\HOME)"
	$destination = Reach-Host "Enter Destination Folder Path: (Excample: \\Thermon.local\USSM\HOME\PSTs)"
	$UserFolders = Get-ChildItem -Path $userFilePath -Directory
	$UserFolders | foreach {
		$user = Get-ADUser -Identity $_ -Properties *
		$homeDir = $userFilePath + "\" + $_
		$filter = "*.pst"
		$n = 1
		$userPSTs = Get-ChildItem -Path $homeDir -Filter $filter -Recurse
		$userPSTs | foreach {
			$fullName = $_.FullName
			$oldName = $_.Name
			$newName = $_.Name
			$newName = $newName.Replace(".pst", "$n.pst")
			Rename-Item -NewName $newName -Path $fullName
			$newName = $user.UserPrincipalName + "_" + $newName
			$fullName = $fullName.Replace("$oldName", "$newName")
			Rename-Item -NewName $newName -Path $fullName
			$n++
			}
		Move-Item -Path $homeDir -Filter $filter -Destination $destination
		## Create Output for PST Mapping File for O365 Import Job ##
		}
	Get-ChildItem -Path $destination -Filter $filter | Out-GridView
	}

If ($choice -eq "File Search")
{
	$File = Read-Host "Enter File Name and Path to Search: (Example: \\Thermon\USSM\ITData\PSTMigration\User_Group1.csv"
	$destination = Reach-Host "Enter Destination Folder Path: (Excample: \\Thermon.local\USSM\HOME\PSTs)"
	$users = Import-Csv -Path $File
	$users | foreach {
		$user = Get-ADUser -Identity $_.samAccountName -Properties *
		$homeDir = $user.HomeDirectory
		$filter = "*.pst"
		$n = 1
		$userPSTs = Get-ChildItem -Path $homeDir -Filter $filter -Recurse
		$userPSTs | foreach {
			$newName = $_.Name
			$oldName = $_.Name
			$fullName = $_.FullName
			$newName = $newName.Replace(".pst", "$n.pst")
			Rename-Item -NewName $newName -Path $fullpath
			$newName = $user.UserPrincipalName + "_" + $newName
			$fullName = $fullName.Replace("$oldName","$newName")
			Rename-Item -NewName $newName -Path $fullName
			$n++
			}
		Move-Item -Path $homeDir -Filter $filter -Destination $destination
		## Create Output for PST Mapping File for O365 Import Job ##	
		}
	Get-ChildItem -Path $destination -Filter $filter | Out-GridView
	}
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

I have updated this code a little but it does now work. I had to move the $fullName=$fullname.Replace("... ") line up to right under the first rename.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PST Find,Rename and Move

Post by jvierra »

I am glad to see that you got it working.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked