2

I am trying to following script to look in our site collection for any sites with title containing "Friday".

I know there should be at least 4 sites that have "Friday" in the site title, but this brings back nothing:

    $varURL = Read-Host -Prompt 'Provide URL to site'
Connect-PnPOnline -Url $varURL -UseWebLogin

Function Get-Subsites() {  
    $subwebs= Get-PnPSubWebs -Recurse | where {$_.Title -contains "FRIDAY" }

     foreach ($Subweb in $subwebs)  
    {  
       Write-Host $Subweb.Title
    }  
}   
Get-SubSites
2

You must use the -like operator, not -contains :

$subwebs= Get-PnPSubWebs -Recurse | where {$_.Title -like "*friday*" }

Contains is used to check if a specific member is part of an array, thus it will only return exact matches to the string.

For example:

❯ $test = @("Friday", "Fridays are the best.", "Yay friday!", "Saturdays are great also.")                                                                                             

❯ $test | where {$_ -contains "friday"}
Friday

❯ $test | where {$_ -like "*friday*" }
Friday
Fridays are the best.
Yay friday!
1
  • thank you...changing to -like worked great! – Sol Web Solutions Apr 17 '20 at 20:27
2

In PowerShell, never use -contains. Instead, use -match. If you make this change your script will work perfectly fine:

$varURL = Read-Host -Prompt 'Provide URL to site'
Connect-PnPOnline -Url $varURL -UseWebLogin

Function Get-Subsites() {  
    $subwebs= Get-PnPSubWebs -Recurse | where {$_.Title -match "FRIDAY" }

     foreach ($Subweb in $subwebs)  
    {  
       Write-Host $Subweb.Title
    }  
}   
Get-SubSites
1

I ran the above the script and it returns the data as expected. couple of things to check.

  • Make sure you are entering the correct url of the Site collection (sometime we enter the tenant root with thought it may go all other site collection, which is not true)
  • try to run this command this way and see if you get anything and if you see Friday in title.

    Connect-PnPOnline -Url "Url of site" -UseWebLogin
    Get-PnPSubWebs -Recurse

*Pnp only connect to one site collection at a given time.

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.