Using PowerShell to convert static URLs in docs in SharePoint

%3CLINGO-SUB%20id%3D%22lingo-sub-775439%22%20slang%3D%22en-US%22%3EUsing%20PowerShell%20to%20convert%20static%20URLs%20in%20docs%20in%20SharePoint%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-775439%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20are%20moving%20a%20site%20collection%20from%20one%20web%20app%20to%20another%2C%20so%20the%20host%20header%20is%20changing.%26nbsp%3B%20Many%20of%20the%20SharePoint%20links%20automatically%20update%20with%20the%20new%20URL%2C%20but%20we%20have%20thousands%20of%20documents%20of%20various%20types%20(.docx%2C%20.pptx%2C%20.js%2C%20.xsn%2C%20etc.)%20in%20libraries%20across%20the%20site%20collection%20that%20have%20static%20links%20pointing%20to%20references%20elsewhere%20in%20the%20site%20collection.%26nbsp%3B%20I'd%20like%20to%20use%20PowerShell%20to%20iterate%20through%20the%20entire%20site%20collection%2C%20find%20all%20documents%20that%20have%20the%20old%20host%20header%20in%20a%20link%2C%20and%20replace%20it%20with%20the%20new%20host%20header.%26nbsp%3B%20I've%20got%20to%20believe%20others%20have%20encountered%20this%20same%20problem%20and%20written%20PS%20code%20to%20solve%20it%2C%20and%20hoping%20someone%20can%20share%20a%20PS%20file%20that%20worked%20for%20this%20task.%26nbsp%3B%20Thanks%20much.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-775439%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-776724%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20convert%20static%20URLs%20in%20docs%20in%20SharePoint%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-776724%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382842%22%20target%3D%22_blank%22%3E%40JimH_6%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20don't%20mention%20the%20version%20of%20SharePoint%20but%20since%20you're%20talking%20about%20different%20webapps%20I%20can%20safely%20assume%20this%20is%20on-premises.%26nbsp%3B%20It%20does%20appear%20to%20be%20possible%20to%20open%20a%20document%20and%20search%20text%20inside%20it%2C%20though%20I%20freely%20admit%20I%20have%20not%20tried%20a%20method%20myself%20to%20verify%20you%20can%20wrap%20SharePoint%20around%20this%20but%20it%20should%20be%20possible.%26nbsp%3B%20Since%20there's%20many%20examples%20of%20how%20to%20loop%20through%20all%20documents%20in%20a%20site%20collection%20I%20won't%20bother%20with%20that%20here%2C%20but%20I%20would%20try%20reviewing%20%3CA%20href%3D%22https%3A%2F%2Fdevblogs.microsoft.com%2Fscripting%2Fweekend-scripter-manipulating-word-and-excel-with-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20example%3C%2FA%3Eon%20a%20single%20document%20in%20a%20library%20to%20see%20if%20it%20works.%26nbsp%3B%20If%20so%2C%20wrap%20that%20code%20in%20foreach%20loops%20(filtering%20out%20lists%20using%20like%20(if%20%24list.basetemplate%20-eq%20%22DocumentLibrary%22)%20and%20other%20necessary%20steps.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDepending%20on%20your%20issue%2C%20this%20issue%20shouldn't%20be%20that%20bad%20if%20you%20have%20SharePoint%202016%20or%20higher%20with%20Office%20Online%20Server%2C%20and%20use%20the%20durable%20link%20instead%20of%20the%20actual%20physical%20link.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

We are moving a site collection from one web app to another, so the host header is changing.  Many of the SharePoint links automatically update with the new URL, but we have thousands of documents of various types (.docx, .pptx, .js, .xsn, etc.) in libraries across the site collection that have static links pointing to references elsewhere in the site collection.  I'd like to use PowerShell to iterate through the entire site collection, find all documents that have the old host header in a link, and replace it with the new host header.  I've got to believe others have encountered this same problem and written PS code to solve it, and hoping someone can share a PS file that worked for this task.  Thanks much.

1 Reply
Highlighted

@JimH_6,

 

You don't mention the version of SharePoint but since you're talking about different webapps I can safely assume this is on-premises.  It does appear to be possible to open a document and search text inside it, though I freely admit I have not tried a method myself to verify you can wrap SharePoint around this but it should be possible.  Since there's many examples of how to loop through all documents in a site collection I won't bother with that here, but I would try reviewing this example on a single document in a library to see if it works.  If so, wrap that code in foreach loops (filtering out lists using like (if $list.basetemplate -eq "DocumentLibrary") and other necessary steps.  

 

Depending on your issue, this issue shouldn't be that bad if you have SharePoint 2016 or higher with Office Online Server, and use the durable link instead of the actual physical link.