|
1 | | - |
2 | | - |
3 | 1 | # Hide SharePoint list from Site Contents |
4 | 2 |
|
5 | 3 | ## Summary |
6 | 4 |
|
7 | | -If you need to hide the SharePoint list from the UI this simple PowerShell script will hide a specific list from the site contents. This will prevent users from easily accessing the list while, for example, you are still setting it up. |
8 | | - |
| 5 | +These scripts hide a SharePoint Online list from Site Contents using PnP PowerShell or CLI for Microsoft 365. The updated PnP PowerShell version also generates an audit record of the administrative action. |
| 6 | + |
| 7 | +The updated version uses PnP PowerShell with certificate-based application authentication to connect to a specified SharePoint Online site, sets the target list's Hidden property to $true, and exports an audit record containing the action date, executing user, site URL, list name, and action performed. |
| 8 | + |
| 9 | +The generated CSV report provides a lightweight, timestamped record suitable for operational auditing and change tracking. |
| 10 | + |
| 11 | +## Why It Matters |
| 12 | + |
| 13 | +SharePoint lists may contain application configuration, integration data, staging information, or other administrative content that should not be exposed through normal SharePoint navigation. |
| 14 | + |
| 15 | +A common administrative scenario is hiding a configuration list from the standard SharePoint user interface while retaining the list and its data for application or administrative use. |
| 16 | + |
| 17 | +This script provides a repeatable approach to: |
| 18 | + |
| 19 | +- Hide a designated SharePoint list without deleting its contents. |
| 20 | +- Perform the change using non-interactive certificate authentication. |
| 21 | +- Record who performed the action and when it occurred. |
| 22 | +- Produce a timestamped audit file for operational records or change management. |
| 23 | + |
| 24 | + |
| 25 | +# [PnP PowerShell Updated (with audit)](#tab/pnppsv2) |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +The following prerequisites apply to the updated PnP PowerShell version with audit reporting. |
| 30 | + |
| 31 | +- PnP.PowerShell module |
| 32 | +- A Microsoft Entra ID application registration |
| 33 | +- Certificate-based authentication configured for the application |
| 34 | +- Appropriate SharePoint permissions granted to the application |
| 35 | +- The certificate corresponding to $ThumbPrint available to the account executing the script |
| 36 | +- Access to the target SharePoint site |
| 37 | +- The target list must already exist |
| 38 | + |
| 39 | +## Configuration |
| 40 | + |
| 41 | +For the updated PnP PowerShell version, configure the variables at the beginning of the script: |
| 42 | + |
| 43 | +| Variable | Description | |
| 44 | +| ----------------- | ---------------------------------------------------------- | |
| 45 | +| `$ClientID` | Azure AD Application (Client) ID | |
| 46 | +| `$ThumbPrint` | Certificate thumbprint | |
| 47 | +| `$Tenant` | Microsoft 365 tenant name (e.g. `contoso.onmicrosoft.com`) | |
| 48 | +| `$SiteUrl` | Your Site URL i.e. `https://contoso.sharepoint.com/sites/A`| |
| 49 | +| `$ListName` | Your List Name i.e. `ListName` | |
| 50 | + |
| 51 | +The script can then be executed from a PowerShell session or incorporated into an administrative automation process. |
| 52 | + |
| 53 | +The executing identity must have sufficient permissions to connect to the SharePoint site and modify the target list. |
| 54 | + |
| 55 | +```powershell |
| 56 | +
|
| 57 | +# ============================================================ |
| 58 | +# CONFIGURATION |
| 59 | +# ============================================================ |
| 60 | +
|
| 61 | +$ClientID = "xxxxxxxxxxxxxxxxxxxxx" |
| 62 | +$ThumbPrint = "xxxxxxxxxxxxxxxxxxxxx" |
| 63 | +$Tenant = "contoso.onmicrosoft.com" |
| 64 | +
|
| 65 | +$SiteUrl = "https://contoso.sharepoint.com/sites/A" |
| 66 | +$ListName = "AppConfig" |
| 67 | +
|
| 68 | +$OutputFolder = "C:\Temp\SharePointListAudit" |
| 69 | +
|
| 70 | +if (-not (Test-Path -LiteralPath $OutputFolder)) { |
| 71 | + New-Item -Path $OutputFolder -ItemType Directory -Force | Out-Null |
| 72 | +} |
| 73 | +
|
| 74 | +$TimeStamp = Get-Date -Format "yyyyMMdd_HHmmss" |
| 75 | +$CsvFile = Join-Path $OutputFolder "SharePointListAudit_$TimeStamp.csv" |
| 76 | +
|
| 77 | +# ============================================================ |
| 78 | +# WHO AND WHEN |
| 79 | +# ============================================================ |
| 80 | +
|
| 81 | +$ActionBy = $env:USERNAME |
| 82 | +$ActionDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" |
| 83 | +
|
| 84 | +# ============================================================ |
| 85 | +# CONNECT |
| 86 | +# ============================================================ |
| 87 | +
|
| 88 | +Connect-PnPOnline ` |
| 89 | + -Url $SiteUrl ` |
| 90 | + -ClientId $ClientID ` |
| 91 | + -Thumbprint $ThumbPrint ` |
| 92 | + -Tenant $Tenant |
| 93 | +
|
| 94 | +# ============================================================ |
| 95 | +# HIDE SHAREPOINT LIST |
| 96 | +# ============================================================ |
| 97 | +
|
| 98 | +Set-PnPList -Identity $ListName -Hidden $true |
| 99 | +
|
| 100 | +# ============================================================ |
| 101 | +# CREATE AUDIT RECORD |
| 102 | +# ============================================================ |
| 103 | +
|
| 104 | +[PSCustomObject]@{ |
| 105 | + ActionDate = $ActionDate |
| 106 | + ActionBy = $ActionBy |
| 107 | + SiteUrl = $SiteUrl |
| 108 | + ListName = $ListName |
| 109 | + Action = "Hide List" |
| 110 | +} | Export-Csv ` |
| 111 | + -Path $CsvFile ` |
| 112 | + -NoTypeInformation ` |
| 113 | + -Encoding UTF8 |
| 114 | +
|
| 115 | +# ============================================================ |
| 116 | +# DISCONNECT |
| 117 | +# ============================================================ |
| 118 | +
|
| 119 | +Disconnect-PnPOnline |
| 120 | +
|
| 121 | +Write-Host "List '$ListName' hidden successfully." |
| 122 | +Write-Host "Audit report: $CsvFile" |
| 123 | +
|
| 124 | +``` |
| 125 | + |
| 126 | +[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] |
| 127 | + |
| 128 | +## Output |
| 129 | + |
| 130 | +The updated PnP PowerShell version creates a timestamped CSV file in the configured output directory: |
| 131 | + |
| 132 | +> SharePointListAudit_yyyyMMdd_HHmmss.csv |
| 133 | +
|
| 134 | +Example record: |
| 135 | + |
| 136 | +### CSV Report Fields |
| 137 | + |
| 138 | +| Field | Description | |
| 139 | +| ----------------------- | ---------------------------------------------------------- | |
| 140 | +| `ActionDate` | Date and time the operation was performed | |
| 141 | +| `ActionBy` | Windows/PowerShell environment username | |
| 142 | +| `SiteUrl` | SharePoint Online site containing the list | |
| 143 | +| `ListName` | List modified by the script | |
| 144 | +| `Action` | Administrative action performed | |
| 145 | + |
| 146 | +## Notes |
| 147 | + |
| 148 | +- Set-PnPList -Hidden $true hides the list; it does not delete the list or its data. |
| 149 | +- The script assumes the target list already exists. |
| 150 | +- $env:USERNAME identifies the local execution context, not necessarily the identity represented by the Entra ID application used for SharePoint authentication. |
| 151 | +- The output directory is created automatically if it does not exist. |
| 152 | +- Disconnect-PnPOnline terminates the PnP PowerShell connection after the operation. |
| 153 | +- For production automation, avoid storing client IDs, certificate material, or other credentials directly in scripts where possible. |
| 154 | +- Consider adding structured error handling and logging if this script is used as part of a larger operational process. |
| 155 | + |
| 156 | +## Refactoring Notes |
| 157 | + |
| 158 | +The updated version adds an alternative using the **PnP PowerShell module (PnP.PowerShell)** with **certificate-based app-only authentication**. The original PnP PowerShell and CLI for Microsoft 365 scripts remain available in their own tabs. |
| 159 | + |
| 160 | +The refactoring also introduces explicit configuration variables, execution metadata, output/audit reporting, structured sections, and clearer operational feedback. |
| 161 | + |
| 162 | +### Improvements Made (v2.0) |
| 163 | + |
| 164 | +| Area | Initial Version | Updated Version | Improvement | |
| 165 | +| ------------------------------- | -------------------------------------- | -------------------------------------------------------------------- | ---------------------------------------------------------- | |
| 166 | +| `Authentication` | m365 login / interactive session | Connect-PnPOnline with Client ID, certificate thumbprint and tenant | Supports unattended/service-style execution | |
| 167 | +| `SharePoint interaction` | m365 spo list set | Set-PnPList | Uses native PnP PowerShell cmdlets | |
| 168 | +| `Configuration` | Site/list values embedded near the top | Dedicated configuration section | Easier to maintain and reuse | |
| 169 | +| `Authentication configuration` | Not explicitly defined | Client ID, Thumbprint and Tenant variables | Makes authentication requirements explicit | |
| 170 | +| `Output` | Console only | Timestamped CSV audit file | Provides persistent execution history | |
| 171 | +| `Audit information` | None | User, date/time, site, list and action | Improves traceability | |
| 172 | +| `Output directory` | None | Automatically created if required | Reduces manual setup | |
| 173 | +| `File naming` | None | Timestamped audit filename | Prevents reports from overwriting each other | |
| 174 | +| `Script structure` | Simple linear script | Clearly separated functional sections | Improves readability and maintenance | |
| 175 | +| `Connection lifecycle` | m365 logout | Disconnect-PnPOnline | Explicitly closes the PnP connection | |
| 176 | +| `User feedback` | Minimal | Success message plus audit-file location | Makes execution outcome clearer | |
| 177 | +| `Encoding` | Not applicable | UTF-8 CSV output | Provides consistent report encoding | |
| 178 | + |
9 | 179 | # [PnP PowerShell](#tab/pnpps) |
10 | 180 |
|
11 | 181 | ```powershell |
@@ -52,17 +222,30 @@ m365 spo list set --webUrl $siteUrl --title $listName --hidden true |
52 | 222 | m365 logout |
53 | 223 |
|
54 | 224 | ``` |
| 225 | + |
55 | 226 | [!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)] |
56 | 227 |
|
| 228 | + |
57 | 229 | *** |
58 | 230 |
|
| 231 | + |
| 232 | + |
59 | 233 | ## Contributors |
60 | 234 |
|
61 | | -| Author(s) | |
| 235 | +|Author(s)| |
62 | 236 | |-----------| |
63 | | -| Leon Armston | |
64 | | -| [Ganesh Sanap](https://ganeshsanapblogs.wordpress.com/about) | |
| 237 | +|Leon Armston| |
| 238 | +|[Ganesh Sanap](https://ganeshsanapblogs.wordpress.com/about)| |
| 239 | +|[Josiah Opiyo](https://github.com/ojopiyo)| |
| 240 | + |
| 241 | +*Built with a focus on automation, governance, least privilege, and clean Microsoft 365 tenants - helping M365 admins gain visibility and reduce operational risk.* |
| 242 | + |
| 243 | +## Version history |
65 | 244 |
|
| 245 | +|Version|Date|Comments| |
| 246 | +|-------|----|--------| |
| 247 | +|1.0|May 09, 2023|Initial release| |
| 248 | +|2.0|October 03, 2026|Refactored version| |
66 | 249 |
|
67 | 250 | [!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] |
68 | | -<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-hide-list-from-site-contents" aria-hidden="true" /> |
| 251 | +<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-hide-list-from-site-contents" aria-hidden="true" /> |
0 commit comments