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?


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')
| improve this answer | |

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
| improve this answer | |

You're missing * in all the file extension tests

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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