I am trying to write a PowerShell script for my SharePoint 2013. The idea is that I what to select all the links that are referencing /documents/ folder, and that ends with either .doc .docx or .pdf. in other words I want all the links that are referencing /documents/ folder and that are either Word or PDF. so I wrote the following If statement :-

 $links = $file.ForwardLinks;
    foreach($link in $links){ 
        if($link.url -like '*/Documents/*' -and ($link.url -like '.doc' -or $link.url -like '.docx'  -or $link.url -like '.pdf') ){

But this will not select any links. Although there are link that contain both /documents/ & .pdf.

Can anyone advice on this please?

3 Answers 3


Wouldn't you need to make this: ($link.url -like '.doc' -or $link.url -like '.docx' -or $link.url -like '.pdf')

like this:

($link.url -like '*.doc' -or $link.url -like '*.docx'  -or $link.url -like '*.pdf')

You can also do the same using

$ext = [System.IO.Path]::GetExtension($link.url)
if($ext -eq '.doc' or $ext -eq '.docx' or $ext -eq '.pdf')
   #file found

You're missing * in all the file extension tests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.