|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - files |
| 4 | + - reports |
| 5 | +--- |
| 6 | + |
| 7 | +# List all files with missing required metadata |
| 8 | + |
| 9 | +Author: [Nico De Cleyre](https://www.nicodecleyre.com), Inspired by [Veronique Lengelle](https://veronicageek.com/2021/find-missing-metadata-using-powershell/) |
| 10 | + |
| 11 | +The following script iterates through all files in all the document libraries of a SharePoint Online site. It returns all the files that have missing required metadata and excludes the `name` metadata |
| 12 | + |
| 13 | +=== "PowerShell" |
| 14 | + |
| 15 | + ```powershell |
| 16 | + $siteUrl = "https://<TENANT-NAME>.sharepoint.com/sites/<YOUR-SITE>" |
| 17 | + $allLists = m365 spo list list --webUrl $($siteURL) --output json | ConvertFrom-Json |
| 18 | + $allLibs = $allLists | where {$_.BaseTemplate -eq 101} |
| 19 | + $results = @() |
| 20 | + $fields = @('FileLeafRef', 'FileSystemObjectType') |
| 21 | + |
| 22 | + foreach ($lib in $allLibs) { |
| 23 | + $allRequiredFields = m365 spo field list --webUrl $($siteURL) --listId $($lib.Id) --query "[?Required == ``true``]" --output json | ConvertFrom-Json |
| 24 | + |
| 25 | + if($allRequiredFields.Length -eq 0){ |
| 26 | + continue |
| 27 | + } |
| 28 | + |
| 29 | + [array]$allRequiredFieldsInternalName = $($allRequiredFields | select InternalName).InternalName |
| 30 | + |
| 31 | + ForEach ($field in $fields) |
| 32 | + { |
| 33 | + If (-not ($allRequiredFieldsInternalName -contains $field)) |
| 34 | + { |
| 35 | + $allRequiredFieldsInternalName += $field |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + $allItems = m365 spo listitem list --webUrl $($siteURL) --listId $($lib.Id) --fields $($allRequiredFieldsInternalName -join ",") --output json | ConvertFrom-Json |
| 40 | + $allItems = $allItems | where {$_.FileSystemObjectType -eq 0} |
| 41 | + |
| 42 | + foreach($item in $allItems){ |
| 43 | + foreach($requiredfield in $allRequiredFields){ |
| 44 | + if($requiredfield.InternalName -eq "FileLeafRef"){ |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + if ($null -eq $item["$($requiredfield.InternalName)"]) { |
| 49 | + $results += [pscustomobject]@{ |
| 50 | + FileName = $item.FileLeafRef |
| 51 | + FieldName = $requiredfield.Title |
| 52 | + ListOrLibrary = $lib.Title |
| 53 | + FileLocation = $lib.RootFolder.ServerRelativeUrl |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + $results |
| 60 | + ``` |
0 commit comments