@@ -4,7 +4,7 @@ title: 'Report on files with unique permissions'
44hero_image : ' /img/IMG_20220521_140146.jpg'
55show_sidebar : false
66hero_height : is-small
7- date : ' 2025-02-09 '
7+ date : ' 2025-02-15 '
88---
99
1010
@@ -53,4 +53,77 @@ $uniqueItems | Export-Csv -Path $outputCsvPath -NoTypeInformation -Encoding UTF8
5353Write-Host "Details of items with unique permissions have been exported to $outputCsvPath"
5454
5555
56+ ```
57+
58+ <br /><br />
59+
60+ # Create a view
61+
62+ With Powershell you can also shows your users all files with unique permissions. The following script creates a view in your library that displays files with different permissions than the entire library.
63+
64+
65+ ``` Powershell
66+
67+ # Define the list name and site URL
68+ $listName = "YourListName"
69+ $siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
70+
71+ # Path for the output CSV file
72+ $outputCsvPath = "C:\Path\To\Output\UniquePermissionsReport.csv"
73+
74+ # Connect to the SharePoint site
75+ Connect-PnPOnline -Url $siteUrl -UseWebLogin
76+
77+ # Check if the boolean column already exists; if not, create it
78+ $columnInternalName = "HasUniquePermissions"
79+ $columnDisplayName = "Has Unique Permissions"
80+
81+ $existingColumns = Get-PnPField -List $listName
82+ if (-not $existingColumns.InternalName -contains $columnInternalName) {
83+ Add-PnPField -List $listName -InternalName $columnInternalName -DisplayName $columnDisplayName -Type Boolean
84+ Write-Host "Boolean column '$columnDisplayName' added to the list."
85+ } else {
86+ Write-Host "Boolean column '$columnDisplayName' already exists."
87+ }
88+
89+ # Get all list items
90+ $items = Get-PnPListItem -List $listName -PageSize 5000 -Fields "FileRef", "FileLeafRef", "Modified", "HasUniqueRoleAssignments"
91+
92+ # Initialize an array to store details of items with unique permissions
93+ $uniqueItems = @()
94+
95+ # Iterate through each item to check for unique permissions and update the new column
96+ foreach ($item in $items) {
97+ $hasUniquePermissions = $item.HasUniqueRoleAssignments
98+
99+ # Add or update the boolean column value
100+ Set-PnPListItem -List $listName -Identity $item.Id -Values @{$columnInternalName = $hasUniquePermissions}
101+
102+ # If the item has unique permissions, add it to the array
103+ if ($hasUniquePermissions) {
104+ $uniqueItems += [PSCustomObject]@{
105+ Name = $item["FileLeafRef"]
106+ FileRef = $item["FileRef"]
107+ LastModified = $item["Modified"]
108+ }
109+ }
110+ }
111+
112+ # Export the details to a CSV file
113+ $uniqueItems | Export-Csv -Path $outputCsvPath -NoTypeInformation -Encoding UTF8
114+
115+ # Check if the view already exists; if not, create it
116+ $viewName = "Unique Permissions View"
117+ $existingViews = Get-PnPView -List $listName
118+ if (-not $existingViews.Title -contains $viewName) {
119+ Add-PnPView -List $listName -Title $viewName -Fields "FileLeafRef", "FileRef", "HasUniquePermissions", "Modified" -Query "<Where><Eq><FieldRef Name='$columnInternalName' /><Value Type='Boolean'>1</Value></Eq></Where>"
120+ Write-Host "View '$viewName' created to display files with unique permissions."
121+ } else {
122+ Write-Host "View '$viewName' already exists."
123+ }
124+
125+ # Output the result
126+ Write-Host "Details of items with unique permissions have been updated and exported to $outputCsvPath"
127+
128+
56129```
0 commit comments