-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFileVersionHistoryReport.ps1
More file actions
215 lines (199 loc) · 8.81 KB
/
GetFileVersionHistoryReport.ps1
File metadata and controls
215 lines (199 loc) · 8.81 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<#
=============================================================================================
Name: Export SharePoint Online File Version History Report Using PowerShell
Version: 1.0
website: o365reports.com
~~~~~~~~~~~~~~~~~~
Script Highlights:
~~~~~~~~~~~~~~~~~~
1. Retrieves file version history for all documents in a site.
2. Exports file version history for a list of sites.
3. Exports version history information for files uploaded by a specific user.
4. Finds files with larger version history based on your input.
5. File size can be exported in preferred units such as MB, KB, B, and GB.
6. Automatically installs the PnP PowerShell module (if not installed already) upon your confirmation.
7. The script can be executed with an MFA-enabled account too.
8. Exports report results as a CSV file.
9. The script is scheduler friendly.
10. It can be executed with certificate-based authentication (CBA) too.
For detailed Script execution: https://o365reports.com/2024/06/20/export-sharepoint-online-file-version-history-report-using-powershell/
============================================================================================
#>
Param
(
[Parameter(Mandatory = $false)]
[string]$AdminName,
[string]$Password,
[String] $ClientId,
[String] $CertificateThumbprint,
[String] $TenantName,
[string]$SiteUrl,
[int]$VersionCount = -1,
[string]$ImportCsv,
[string]$UserId,
[ValidateSet('GB', 'MB', 'KB', 'B')]
[string]$Unit
)
Function Installation-Module{
$Module = Get-InstalledModule -Name PnP.PowerShell -RequiredVersion 1.12.0 -ErrorAction SilentlyContinue
If($Module -eq $null){
Write-Host PnP PowerShell Module is not available -ForegroundColor Yellow
$Confirm = Read-Host Are you sure you want to install module? [Yy] Yes [Nn] No
If($Confirm -match "[yY]") {
Write-Host "Installing PnP PowerShell module..."
Install-Module PnP.PowerShell -RequiredVersion 1.12.0 -Force -AllowClobber -Scope CurrentUser
Import-Module -Name Pnp.Powershell -RequiredVersion 1.12.0
}
Else{
Write-Host PnP PowerShell module is required to connect SharePoint Online.Please install module using Install-Module PnP.PowerShell cmdlet.
Exit
}
}
Write-Host `nConnecting to SharePoint Online...
}
Function Connection-Module{
param
(
[Parameter(Mandatory = $true)]
[String] $Url
)
if(($AdminName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $AdminName,$SecuredPassword
Connect-PnPOnline -Url $Url -Credential $Credential
}
elseif($TenantName -ne "" -and $ClientId -ne "" -and $CertificateThumbprint -ne "")
{
Connect-PnPOnline -Url $Url -ClientId $ClientId -Thumbprint $CertificateThumbprint -Tenant "$TenantName.onmicrosoft.com"
}
else
{
Connect-PnPOnline -Url $Url -interactive
}
}
Function Convert-Bytes {
$Bytes = switch ($Unit.ToUpper()) {
'GB' { 1GB }
'MB' { 1MB }
'KB' { 1KB }
'B' { 1 }
Default {
Write-Host "Invalid unit is provided. csv will display file sizes in megabyte"
1MB
}
}
return $Bytes
}
Function HumanReadableByteSize ($Size) {
Switch ($Size) {
{$_ -gt 1TB} {$Size = ($Size / 1TB).ToString("n2") + " TB";break}
{$_ -gt 1GB} {$Size = ($Size / 1GB).ToString("n2") + " GB";break}
{$_ -gt 1MB} {$Size = ($Size / 1MB).ToString("n2") + " MB";break}
{$_ -gt 1KB} {$Size = ($Size / 1KB).ToString("n2") + " KB";break}
default {$Size = "$Size B"}
}
Return $Size
}
Function Store-FileVersionInformation($FileItem){
write-host $fileItem.FieldValues.FileRef -f Green
If($Versions.Count -ge $VersionCount){
$VersionSize = $Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
$VersionSize = [Math]::Round(($VersionSize/$bytes),2)
$FileSize = [Math]::Round(($FileItem.FieldValues.File_x0020_Size/$Bytes),2)
$TotalFileSize = $FileSize + $VersionSize
$FileVersionData = [PSCustomObject][Ordered]@{
"Site" = $Site
"Library" = $List.Title
"File Name" = $FileItem.FieldValues.FileLeafRef
"File URL" = $File.ServerRelativeUrl
"Major Versions" = $File.MajorVersion
"Minor Versions" = $file.MinorVersion
"Versions Count" = $Versions.Count
"File Size" = HumanReadableByteSize ($FileSize*$Bytes)
"Version History Size" = HumanReadableByteSize ($VersionSize*$Bytes)
"Total File Size" = HumanReadableByteSize ($TotalFileSize*$Bytes)
"File Size ($unit)" = $FileSize
"Version History Size ($unit) " = $VersionSize
"Total File Size ($unit)" = $TotalFileSize
"Created By" = $FileItem.FieldValues.Author.Email
"Created Date" = $File.TimeCreated
"Modified By" = $FileItem.FieldValues.Editor.Email
"Last Modified" = $file.TimeLastModified
}
$FileVersionData | Export-Csv -Path $ReportOutput -NoTypeInformation -Append -force
$Global:ItemCount++
}
}
Function Get-SiteFileVersion($DocumentLibraries){
ForEach($List in $DocumentLibraries)
{
$Files = Get-PnPListItem -List $List -PageSize 2000 -Fields File_x0020_Size, FileRef, Author | Where {$_.FileSystemObjectType -eq "File"}
Foreach($FileItem in $Files){
Write-Progress -Activity ("Site : "+$Site +" | List : "+$List.Title) -Status ("Processing Item: "+$FileItem.FieldValues.FileLeafRef)
$File = Get-PnPProperty -ClientObject $FileItem -Property File
$Versions = Get-PnPProperty -ClientObject $File -Property Versions
If($UserId -ne "" -and $UserId -eq $FileItem.FieldValues.Author.Email){
Store-FileVersionInformation $FileItem
}
ElseIf($UserId -eq ""){
Store-FileVersionInformation $FileItem
}
}
}
}
If($Unit -ne ""){
$Bytes = Convert-Bytes
}
else{
Write-Host -f cyan "Note: By default file sizes will be shown in MB."
Write-Host -f cyan "You can specify the unit[B, KB, MB, GB] by passing the desired unit using '-Unit' argument."
$Bytes = 1MB
$Unit = "MB"
}
Installation-Module
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$ReportOutput = "$PSScriptRoot\SPO File Version History Report $timestamp.csv"
$Global:ItemCount = 0
If($ImportCsv -ne ""){
$ListOfSites = Import-csv -Path $ImportCsv
Foreach($Site in $ListOfSites){
Connection-Module -Url $Site.SiteUrl
$Site = (Get-PnPWeb | Select Title).Title
$ExcludedLists = @("Form Templates","Style Library","Site Assets","Site Pages", "Preservation Hold Library", "Pages", "Images",
"Site Collection Documents", "Site Collection Images")
$DocumentLibraries = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists -and $_.BaseType -eq "DocumentLibrary"}
Get-SiteFileVersion $DocumentLibraries
Disconnect-PnPOnline
}
}
Else{
If($SiteUrl -eq ""){
$SiteUrl = Read-Host "Site Url "
}
Connection-Module -Url $SiteUrl
$Site = (Get-PnPWeb | Select Title).Title
$ExcludedLists = @("Form Templates","Style Library","Site Assets","Site Pages", "Preservation Hold Library", "Pages", "Images",
"Site Collection Documents", "Site Collection Images")
$DocumentLibraries = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists -and $_.BaseType -eq "DocumentLibrary"}
Get-SiteFileVersion $DocumentLibraries
Disconnect-PnPOnline
}
if((Test-Path -Path $ReportOutput) -eq "True")
{
Write-Host `nThe output file contains $Global:ItemCount files
Write-Host `n The Output file availble in: -NoNewline -ForegroundColor Yellow
Write-Host $OutputCSV
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
$Prompt = New-Object -ComObject wscript.shell
$UserInput = $Prompt.popup("Do you want to open output file?",`
0,"Open Output File",4)
If ($UserInput -eq 6)
{
Invoke-Item "$ReportOutput"
}
}
else{
Write-Host -f Yellow "No Records Found"
}