Multiple AND OR inside a powershell if statment
-
03-10-2020 - |
Question
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?
Solution
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')
OTHER TIPS
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
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange