This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I'm extremely new to PowerShell so be gentle. I'm also not entirely sure where this should be posted but I figured I would start here. I've got a link to my post on Stackoverflow; feel free to comment here or there. I could just really use some eyes on this.
#sharepoint site url
$Site = Get-SPOSite https://______________________
#variables
$CountDocumentItems = 0
$CountDocumentLibray = 0
$CountListItems = 0
$CountList = 0
$Results = @()
#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential
Function Get-SPOWeb($WebURL){
#Setup credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get Web information and subsites
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
$Context.Credentials = $Credentials
$Web = $Context.Web
$Context.Load($Web)
$Context.Load($Web.Webs)
$Context.executeQuery()
#Iterate through each subsite in the current web
foreach ($Subweb in $Web.Webs) {
#Get the web object
$Subweb
#Call the function recursively to process all subsites underneath the current web
Get-SPOWeb($Subweb.url)
}
}
$AllWebs = Get-SPOWeb $Site.Url
foreach ($item in $AllWebs) {
foreach($list in $item.Lists) {
if ($list.BaseType -eq "DocumentLibrary") {
$CountDocumentLibray = $CountDocumentLibray 1
$CountDocumentItems = $list.ItemCount $CountDocumentItems
}
else {
$CountList = $CountList 1
$CountListItems = $list.ItemCount $CountListItems
}
}
$Results = New-Object PSObject -Property @{'Site Title' = $item.Title
'Document Library Count' = $CountDocumentLibray
'Document Count' = $CountDocumentItems
'List Count' = $CountList
'List Item Count' = $CountListItems
}
$CountDocumentItems = 0
$CountDocumentLibray = 0
$CountListItems = 0
$CountList = 0
}
#$site.Dispose()
$Results | Select 'Site Title', 'Document Library Count', 'Document Count', 'List Count', 'List Item Count' | Format-Table
When I run this it does not like the
foreach($list in $item.Lists)
line and gives me alternating "The collection has not been initialized" errors for $item.Lists and $list. any ideas?
Edit: My coworker suggested I paste my code into chatgpt and it did get my code working! I edited the stack overflow post to include what chatgpt suggested I add. What a time to be alive.
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/sharepoint/...