Search code examples
imagepowershellsharepoint-2010

Extracting image path in PowerShell


I am finding the images path which are coming from the _layouts folder only,

like i have 2 images on the page but i need to extract path(src) of the image which is in the layouts folder in powershell

<img alt="Smiley face" src="/sites/230020/PublishingImages/Slide%202.png"/> 
<img alt="Smiley face" src="/sites/230020/_layouts/Images/Slide.png"/>

i have used the below it is working

$ImagePath = $WebpartContentXml.SubString($WebpartContentXml.IndexOf("_") - 1, $WebpartContentXml.LastIndexOf(".") - $WebpartContentXml.IndexOf("_") + 5)

but it might break in some cases if the images has extension with 4 characters or we have more than one image on the page with path having "_"


Solution

  • You could use a regex:

    [regex]::Matches($WebpartContentXml, '(_layouts[^"]+)') | ForEach-Object {$_.Groups[1].value}