Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

1
Trying to make a powershell script to get number of lists, list items, libraries, and documents in a given sharepoint online site
Post Body

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.

Author
Account Strength
100%
Account Age
6 years
Verified Email
Yes
Verified Flair
No
Total Karma
12,723
Link Karma
6,698
Comment Karma
5,649
Profile updated: 4 days ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago