Update URLs in an Excel Column
This PowerShell script automates the process of updating URLs in an Excel column, particularly useful for SharePoint migrations or when consolidating multiple domains into a single platform. The script reads an Excel file, finds URLs matching a pattern, and updates them with new domain information.
Purpose
This script is designed to help with:
- SharePoint migration URL updates
- Domain consolidation projects
- Bulk URL modifications in Excel data
- Redirecting legacy links to new locations
- Updating hyperlink references after site restructuring
Prerequisites
- PowerShell 5.1 or later
- ImportExcel PowerShell module (Install-Module ImportExcel)
- Read/write permissions to the target Excel file
- Excel file with URL column to be updated
PowerShell Script
# Load the Excel file
$excelPath = "C:\path\to\your\file.xlsx"
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$workbook = $excel.Workbooks.Open($excelPath)
$worksheet = $workbook.Sheets.Item("YourWorksheetName")
# Get the last row with data in Column C (or change column number as needed)
$lastRow = $worksheet.Cells($worksheet.Rows.Count, 3).End(-4162).Row
Write-Host "Processing $lastRow rows..." -ForegroundColor Cyan
# Loop through Column C and update hyperlinks
$count = 0
for ($i = 2; $i -le $lastRow; $i++) {
$cell = $worksheet.Cells.Item($i, 3)
# Check if cell has a hyperlink
if ($cell.Hyperlinks.Count -gt 0) {
$hyperlink = $cell.Hyperlinks.Item(1)
$oldUrl = $hyperlink.Address
$updated = $false
$newUrl = $oldUrl
# Check for URL-encoded version (Old%20Value)
if ($oldUrl -like "*Old%20Value*") {
$newUrl = $oldUrl -replace "Old%20Value", "New%20Value"
$updated = $true
}
# Check for space version (Old Value)
elseif ($oldUrl -like "*Old Value*") {
$newUrl = $oldUrl -replace "Old Value", "New Value"
$updated = $true
}
# Check for additional pattern (Value you want to replace)
elseif ($oldUrl -like "*Value you want to replace*") {
$newUrl = $oldUrl -replace "Value you want to replace", "Replacement Value"
$updated = $true
}
# Add more replacement patterns as needed
elseif ($oldUrl -like "*AnotherOldValue*") {
$newUrl = $oldUrl -replace "AnotherOldValue", "AnotherNewValue"
$updated = $true
}
if ($updated) {
$hyperlink.Address = $newUrl
$count++
Write-Host "Row $i - UPDATED" -ForegroundColor Green
Write-Host " Old: $oldUrl" -ForegroundColor Gray
Write-Host " New: $newUrl" -ForegroundColor Cyan
}
}
}
Write-Host "`nTotal URLs updated: $count" -ForegroundColor Yellow
# Save and close
$workbook.Save()
$workbook.Close()
$excel.Quit()
# Clean up COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Write-Host "`nDone! File saved successfully." -ForegroundColor Green
Usage Instructions
- Install ImportExcel Module: Run
Install-Module ImportExcelif not already installed - Update File Path: Set
$ExcelFilePathto your target Excel file - Configure Parameters: Update worksheet name, column name, and domain values
- Run Script: Execute the PowerShell script
- Verify Results: Check the output and verify updated URLs
Safety Features
- Automatic Backup: Creates backup copy before making changes
- Error Handling: Includes try-catch blocks for safe execution
- Progress Reporting: Shows which URLs are being updated
- Result Verification: Displays summary of changes made
- Selective Updates: Only updates URLs containing the old domain
Common Use Cases
- SharePoint Migration: Update links from old tenant to new tenant
- Domain Changes: Change company domain in URL references
- Site Consolidation: Merge multiple site collections
- Environment Migration: Move from development to production URLs
- Link Cleanup: Standardize URL formats across datasets
Customization Options
- Modify regex pattern for complex URL transformations
- Add multiple domain replacements in a loop
- Include additional validation before URL updates
- Export results to separate report file
- Process multiple worksheets in the same file