Posted in : Microsoft, Office 365, Powershell By Oliwer Sundgren Translate with Google ⟶

1 year ago

I recently got tasked  with compiling a list of all the MP4 and MOV files in a SharePoint site to see how many files there were, where they all were located in the site and also how big each file was.
So naturally I turned to Powershell and put together a script for this, and I’d love to Share that with you!

(I will put the script in step by step instructions if you want to know what each part does, if you just want the complete script keep scrolling a bit more 😊 )

First we need some variables to keep track of what site we are connecting to, what filetype to look for and a file path so we can export our results to a CSV file at the end.

$SiteURL = ”https://domain.sharepoint.com/sites/mysite”

$CSVFile = ”C:\Reports\MP4Files.csv”

$FileType = ”mp4”

After we have set the variables we also need to connect to the Sharepoint site over the PNP module and also look for all of the document libraries in the site. BaseTemplate 101 is the reference code for “Document Library” which we will use to filter out all document libraries.

Connect-PnPOnline -Url $SiteURL -Interactive

$DocLibraries = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101}

Now we get to the fun stuff! Using a PSCustomObject called $Results and a Foreach loop together with an IF Statement we will now scan all document libraries in the $DocLibraries variable and retrieve all the “.MP4” files, compile that to a nice list via PSCustomObject.

$results = @()

foreach ($DocLibrary in $DocLibraries) {
$AllItems = Get-PnPListItem -PageSize 1000 -List $DocLibrary

foreach ($Item in $AllItems) {
if ($Item[”FileLeafRef”] -like ”*.$FileType”) {

$results += [pscustomobject][ordered] @{

ID       = $Item[”ID”]
FileName = $Item[”FileLeafRef”]
FilePath = $Item[”FileRef”]
SizeInMB = [Math]::Round(($Item[”File_x0020_Size”]/1MB),2)

}

          }#end of IF statement
      }
}

And now that we have gathered all the files, their name, path/location in sharepoint and also their size in MB we are now ready to export the results to our CSV file.

$Results | Export-Csv -Path $CSVFile -NoTypeInformation

The exported results will look something like this in the CSV file ( I advise using Excel to view CSV Files since its easier to read.

Thank you for reading through this post, I hope it helps you, below you have the complete script you can just copy it and edit the variables with your information.

If you have questions or don’t get the script to work feel free to comment or email me at Oliwer.sundgren@xenit.se

 

#Variables#

       $SiteURL = ”https://domain.sharepoint.com/sites/mysite”
       $CSVFile = ”C:\Reports\MP4Files.csv”
       $FileType = ”mp4”

#Connect to SharePoint PNP#

Connect-PnPOnline -Url $SiteURL -Interactive

#Get all Document Libraries in site and continue with script#

$DocLibraries = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101}

        $results = @()

foreach ($DocLibrary in $DocLibraries) {
$AllItems = Get-PnPListItem -PageSize 1000 -List $DocLibrary

foreach ($Item in $AllItems) {
if ($Item[”FileLeafRef”] -like ”*.$FileType”) {

$results += [pscustomobject][ordered] @{

ID                = $Item[”ID”]
FileName = $Item[”FileLeafRef”]
FilePath   = $Item[”FileRef”]
SizeInMB = [Math]::Round(($Item[”File_x0020_Size”]/1MB),2)

}

          }#end of IF statement
      }
}
$Results | Export-Csv -Path $CSVFile -NoTypeInformation

 

 

 

 

Tags : How to, Microsoft, Office 365, PowerShell, SharePoint

Personlig rådgivning

Vi erbjuder personlig rådgivning med författaren för 1400 SEK per timme. Anmäl ditt intresse i här så återkommer vi så snart vi kan.

Add comment

Your comment will be revised by the site if needed.