Forum Discussion

RahamimL's avatar
RahamimL
Iron Contributor
Feb 20, 2020
Solved

Get Sharepoint site file count with powershell

Hi everyone,

 

I hope this is something really stupid and can be done easily. I was tasked with retrieving all Microsoft Teams that were opened since a specific date. In the report, I need to have team name and mail, date created, date modified and file count.

I got everything except the file count. And, I don't want to use pnp module because I was able to get my data with the SharePoint online module.

can someone point me in the right direction?

 

Thanks, Rahamim.

  • In the end this is what I wrote:

    $credentials = Get-Credential (Privileged user in tenant)
    Import-Module ExchangeOnline
    Import-Module MicrosoftTeams
    Import-Module Microsoft.Online.SharePoint.PowerShell
    Connect-SPOService -Url "https://(Tenenat)-admin.sharepoint.com " -Credential $credentials
    Connect-MicrosoftTeams -Credential $credentials
    Connect-ExchangeOnline -Credential $credentials
    $groups = Get-EXORecipient -RecipientTypeDetails GroupMailbox -Properties WhenCreated | ? WhenCreated -ge ((Get-Date (x.x.xxxx)).ToString())
    Write-Host "Found" $groups.Count "Groups, Getting search team by group..." -ForegroundColor Green
    $table = $null
    $Table = New-Object System.Collections.ArrayList
    foreach($group in $groups){
        $Line = New-Object -TypeName psobject
        if(Get-Team -MailNickName $group.Alias){
            Write-host "Found Team" $group.DisplayName "with alias" $group.Alias "getting data..." -ForegroundColor Green
            Add-Member -InputObject $Line -MemberType NoteProperty -Name "DisplayName" -Value $group.DisplayName
            Add-Member -InputObject $Line -MemberType NoteProperty -Name "Email" -Value $group.PrimarySmtpAddress
            Add-Member -InputObject $Line -MemberType NoteProperty -Name "WhenCreated" -Value $group.WhenCreated.ToString()
            $Sitecheck = "https://(Tenant).sharepoint.com/sites/" + $group.Alias
            if((Get-SPOSite -Limit all | ? Url -eq $Sitecheck) -ne $null){$Site = Get-SPOSite $Sitecheck | select *}else{$Site = Get-SPOSite -Limit all | ? Url -like ($Sitecheck + "*") | select *}
            $Site.Url
            Add-Member -InputObject $Line -MemberType NoteProperty -Name "DateModified" -Value $Site.LastContentModifiedDate.ToString()
            Add-Member -InputObject $Line -MemberType NoteProperty -Name "UsedStorageMB" -Value $Site.StorageUsageCurrent.ToString()
            [void]$Table.Add($Line)
            }
        }
    $Table | Export-Csv C:\Temp\MS-Teams.csv -NoTypeInformation -Encoding UTF8

    • RahamimL's avatar
      RahamimL
      Iron Contributor

      Alireza Rahimifaridas I wrote, I need a report for all those site. I don't want to do it manually and I might be required to do this again. That is why I need PowerShell. I have to use Exchange to find the group associated with the Team, and when it was created. And SharePoint, to get the date modified parameter and the amount of files. The only problem is with the file count.

       

      Rahamim. 

      • RahamimL's avatar
        RahamimL
        Iron Contributor

        In the end this is what I wrote:

        $credentials = Get-Credential (Privileged user in tenant)
        Import-Module ExchangeOnline
        Import-Module MicrosoftTeams
        Import-Module Microsoft.Online.SharePoint.PowerShell
        Connect-SPOService -Url "https://(Tenenat)-admin.sharepoint.com " -Credential $credentials
        Connect-MicrosoftTeams -Credential $credentials
        Connect-ExchangeOnline -Credential $credentials
        $groups = Get-EXORecipient -RecipientTypeDetails GroupMailbox -Properties WhenCreated | ? WhenCreated -ge ((Get-Date (x.x.xxxx)).ToString())
        Write-Host "Found" $groups.Count "Groups, Getting search team by group..." -ForegroundColor Green
        $table = $null
        $Table = New-Object System.Collections.ArrayList
        foreach($group in $groups){
            $Line = New-Object -TypeName psobject
            if(Get-Team -MailNickName $group.Alias){
                Write-host "Found Team" $group.DisplayName "with alias" $group.Alias "getting data..." -ForegroundColor Green
                Add-Member -InputObject $Line -MemberType NoteProperty -Name "DisplayName" -Value $group.DisplayName
                Add-Member -InputObject $Line -MemberType NoteProperty -Name "Email" -Value $group.PrimarySmtpAddress
                Add-Member -InputObject $Line -MemberType NoteProperty -Name "WhenCreated" -Value $group.WhenCreated.ToString()
                $Sitecheck = "https://(Tenant).sharepoint.com/sites/" + $group.Alias
                if((Get-SPOSite -Limit all | ? Url -eq $Sitecheck) -ne $null){$Site = Get-SPOSite $Sitecheck | select *}else{$Site = Get-SPOSite -Limit all | ? Url -like ($Sitecheck + "*") | select *}
                $Site.Url
                Add-Member -InputObject $Line -MemberType NoteProperty -Name "DateModified" -Value $Site.LastContentModifiedDate.ToString()
                Add-Member -InputObject $Line -MemberType NoteProperty -Name "UsedStorageMB" -Value $Site.StorageUsageCurrent.ToString()
                [void]$Table.Add($Line)
                }
            }
        $Table | Export-Csv C:\Temp\MS-Teams.csv -NoTypeInformation -Encoding UTF8

Resources