2

I'm building a PowerShell script to get the url's for my SharePoint sites' pages (see below)

$siteURL = "http://mysite"
$site = new-object microsoft.sharepoint.spsite($siteURL)
$webApp = $site.webapplication
$allSites = $webApp.sites
foreach ($site in $allSites) {
    $allWebs = $site.AllWebs
    foreach ($web in $allWebs) {
        $lists = $web.Lists
        #Looking for lists of type Site Pages
        if($list.Title -eq "Site Pages") {
            # Going through all the pages in a Site Pages List
            foreach ($item in $list.Items) {
                $pageURL = XXXXXXXX
            }
        }
    }

I'm struggling a bit in the last nested foreach and I wonder:

  • Is $list.Title -eq "Site Pages" the best way to identify a list of that "type"?
  • Once I'm iterating the Site Pages list items, how can I set the &pageURL variable?
1

You can safely use $list.Title to identify a list. Since "Site Pages" is a Document library, you can use extra check to identify the type of list. I reowrked your code to use Pipes. Here is an example:

Get-SPSite -WebApplication http://mysite -Limit All |
Select -ExpandProperty AllWebs |
Select -ExpandProperty Lists |
Where { $_.GetType().Name -eq "SPDocumentLibrary" -and $_.Title -eq "Site Pages"} |
Select -ExpandProperty Items |
Select Name, Url 

UPDATE

$siteURL = "http://yoursite"
$site = new-object Microsoft.Sharepoint.SPSite($siteURL)
$webApp = $site.WebApplication
$allSites = $webApp.Sites
foreach ($site in $allSites) 
{
    $allWebs = $site.AllWebs
    foreach ($web in $allWebs) 
    {
        $lists = $web.Lists
        foreach($list in $lists)
        {
            #Looking for lists of type Site Pages
            if($list.Title -eq "Site Pages" -and  $list.BaseType -eq "DocumentLibrary") 
            {               
                $items = $list.Items
                # Going through all the pages in a Site Pages List
                foreach ($item in $items) 
                {                   
                    $pageURL = $item.Url
                    Write-Host $pageURL
                }
            }           
        }        
    }
}
  • Thanks @nadeem-yousuf. I am new to PowerShell and will prefer to stick to a code without using pipes. Unless, there are a way to use pipes in a function inside my code. Thanks again. – John Casablanca Mar 20 '15 at 6:14
  • @JohnCasablanca, see my updated answer. – Nadeem Yousuf Mar 20 '15 at 6:49
  • Great, I will add the $list.BaseType -eq "DocumentLibrary" condition. Answer accepted :) – John Casablanca Mar 22 '15 at 21:47

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.