If you need to get all pages (.aspx) within a site collection, I have created a small script. The script enumerates through a site collection (sub-sites included) and retrieves all .aspx pages.
Before executing the script there are some requirements:
- You need to be administrator on the site collection.
- Install the PnP PowerShell module for SharePoint (the script was tested with SharePoint Online).
There are 3 variables you need to modify on line 32 (site collection URL), 33 (SharePoint root URL), 34 (username) and 35 (password).
The script will generate a report (when finished) on the desktop of the user running the script containing the URL, author and last modified date of the page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
#requires -version 3 <# .SYNOPSIS . .DESCRIPTION . .NOTES Version: 1.0 Author: Alex Ø. T. Hansen (ath@tofte-it.dk) Creation Date: 03-12-2018 Purpose/Change: Initial script development #> #region begin boostrap ############### Bootstrap - Start ############### #Clear the screen. Clear-Host; #Import module. Import-Module SharePointPnPPowerShellOnline; ############### Bootstrap - End ############### #endregion #region begin input ############### Input - Start ############### #SharePoint Online. $SPOServiceUrl = "https://contoso.sharepoint.com/sites/somesite"; $SPOServiceRootUrl = "https://contoso.sharepoint.com"; $SPOServiceUsername = "myuser@contoso.com"; $SPOServicePassword = "<Password goes here>"; #Files. $Files = @{ "Report" = "C:\Users\$($env:USERNAME)\Desktop\Pages.csv"; }; ############### Input - End ############### #endregion #region begin functions ############### Functions - Start ############### #Creates a PS credential object. Function Create-PSCredential { [cmdletbinding()] Param ( [Parameter(Mandatory=$true, HelpMessage="Please provide a valid username, example 'Domain\Username'.")]$Username, [Parameter(Mandatory=$true, HelpMessage="Please provide a valid password, example 'MyPassw0rd!'.")]$Password ) #Convert the password to a secure string. $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force; #Convert $Username and $SecurePassword to a credential object. $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecurePassword; #Return the credential object. Return $Credential; } #Get all .ASPX pages. Function Get-SPSitePages { [cmdletbinding()] Param ( [Parameter(Mandatory=$true)]$Session, [Parameter(Mandatory=$true)]$RootURL, [Parameter(Mandatory=$true)][ValidateSet("Yes","No")]$Subsite ) #Object array. $Pages = @(); #Get all lists. $Lists = Get-PnPList -Connection $Session; #Foreach lists. Foreach($List in $Lists) { #Get all list items. $ListItems = Get-PnPListItem -Connection $Session -List $List.Title; #If there is any list items. If($ListItems) { #Foreach list item. Foreach($ListItem in $ListItems) { #If the site is a .ASPX site. If($ListItem.FieldValues.File_x0020_Type -eq "aspx") { #Create new object. $Page = New-Object -TypeName PSObject; #Add data to the object. Add-Member -InputObject $Page -MemberType NoteProperty -Name Url -Value ($RootURL + $ListItem.FieldValues.FileRef); Add-Member -InputObject $Page -MemberType NoteProperty -Name Author -Value ($ListItem.FieldValues.Author.LookupValue); Add-Member -InputObject $Page -MemberType NoteProperty -Name LastModified -Value (($ListItem.FieldValues.Modified).ToString()); Add-Member -InputObject $Page -MemberType NoteProperty -Name Subsite -Value ($Subsite); #Add object to array. $Pages += $Page; } } } } #Return pages. Return $Pages; } #Write to the console. Function Write-Console { [cmdletbinding()] Param ( [Parameter(Mandatory=$false)][string]$Category, [Parameter(Mandatory=$false)][string]$Text ) #If the input is empty. If([string]::IsNullOrEmpty($Text)) { $Text = " "; } #If category is not present. If([string]::IsNullOrEmpty($Category)) { #Write to the console. Write-Output("[" + (Get-Date).ToString("dd/MM-yyyy HH:mm:ss") + "]: " + $Text + "."); } Else { #Write to the console. Write-Output("[" + (Get-Date).ToString("dd/MM-yyyy HH:mm:ss") + "][" + $Category + "]: " + $Text + "."); } } ############### Functions - End ############### #endregion #region begin main ############### Main - Start ############### #Connect to the SharePoint environment. Write-Console -Text ("Connecting to the SharePoint site '" + $SPOServiceUrl + "'"); $PNPSession = Connect-PNPOnline -Url $SPOServiceUrl -Credentials (Create-PSCredential -Username $SPOServiceUsername -Password $SPOServicePassword) -ReturnConnection; #Object array. $Pages = @(); #Get all pages. Write-Console -Text ("Getting pages (.aspx) from '" + $SPOServiceUrl + "', please wait"); $Pages += Get-SPSitePages -Session $PNPSession -Subsite No -RootURL $RootURL; #Get all subsites. $Subsites = Get-PnPSubWebs -Recurse -Connection $PNPSession; #Foreach subsite. Foreach($Subsite in $Subsites) { #Connect to the SharePoint subsite. Write-Console -Text ("Connecting to the SharePoint subsite '" + $RootURL + $Subsite.ServerRelativeUrl + "'"); $PNPSessionSubsite = Connect-PNPOnline -Url ($RootURL + $Subsite.ServerRelativeUrl) -Credentials (Create-PSCredential -Username $SPOServiceUsername -Password $SPOServicePassword) -ReturnConnection; #Get all pages. Write-Console -Text ("Getting pages (.aspx) from '" + ($RootURL + $Subsite.ServerRelativeUrl) + "', please wait"); $Pages += Get-SPSitePages -Session $PNPSessionSubsite -Subsite Yes -RootURL $RootURL; } #Export to the .CSV file. $Pages | Export-Csv -Path $Files.Report -Encoding UTF8 -Delimiter ";" -NoTypeInformation; ############### Main - End ############### #endregion #region begin finalize ############### Finalize - Start ############### #Disconnect the PowerShell session. Disconnect-PnPOnline -Connection $PNPSession; Disconnect-PnPOnline -Connection $PNPSessionSubsite; ############### Finalize - End ############### #endregion |
Please suggest if this script work in sharepoint on premises. I want to collect the same this.
If yes, what command i need to change or add.
Please reply and suggest
Not sure. I think it may need some adjustment