Reset unique permission for SharePoint document library and files

Sometime we need to reset the unique permission for document library, sub folders and files. If the document library has only 1-2 documents then we can handle it manually, but if you have thousands of files and folder so its quite difficult to reset it for all one by one. Below piece of code can help to reset unique permission for document library and its content. This script will take the top level permission from document library.


function ResetUniquePermission {
Param(
[Parameter(Mandatory = $true, HelpMessage="Enter the site url")][ValidateNotNullorEmpty()][string] $SiteURL ,
[Parameter(Mandatory = $true, HelpMessage="Enter the library name")][ValidateNotNullorEmpty()][string] $LibraryName,
[Parameter(Mandatory = $true, HelpMessage="Total number of files")][ValidateNotNullorEmpty()][int] $TotalFiles

)

$Web = Get-SPWeb $SiteURL 
$List= $Web.Lists[$LibraryName]
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.ViewXml =  @"
<View Scope="RecursiveAll">
    <Query>
        <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
    </Query>
    <RowLimit Paged="TRUE">$TotalFiles</RowLimit>
</View>
"@;

$Items = $List.GetItems($Query)
for($j=0;$j -lt $Items.Count ;$j++)
{
    $Items[$j].ResetRoleInheritance()
    Write-Host "Permission reset done for file "$Items[$j]["Title"] -ForegroundColor Green
}
}

SharePoint Installation Error: Windows cannot find e:\prerequisiteinstaller.exe

To install SharePoint we attaching SharePoint setup ISO. It seems due to the same after installation complete, it is looping this error after login to server.

This issue I face in SharePoint 2013 to SharePoint 2019 and the solution will work for all the version mentioned. So just run the below powershell script it will delete the .CMD file from startup and after that command popup won’t appear after login to server.

$path="C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
$file= Get-ChildItem $path -Recurse -Force -Filter SharePointServerPreparationToolStartup* | Where-Object {!$_.PSIsContainer}
if($file -ne $null) 
{
	$file | Remove-Item
	Write-Host "File $($file.Name) removed successfully" -ForegroundColor green
}
else
{
   Write-Host "File not found" -ForegroundColor red
}

Get error line number powershell program

If someone wrote a PowerShell program, with multiple lines and without caring the coding standard and you got stuck to find where the error is, so very simplest way which I am thinking is just use below piece of code which saves your time and let you know about a line which is having an issue.

InvocationInfo can provide information about how and where this command was invoked

So just use simple try-catch like below

try {
    #&lt;replace with your code&gt;
}
catch {
    $_ |select -expandproperty invocationinfo
    write-host "$($_.Exception.Message)" -foregroundcolor red  
    break;
}

Publish multiple pages in SharePoint

As a SharePoint admin most of the time we are getting requirement to publish pages or while working on migration for some cases we would require to publish pages. Here is the small piece of code which can help to publish single page or multiple pages using Powershell.

This piece of code is required for both

$LibraryTitle = "<Replace with Page Library Name>"
$SiteURL = "<Replace with your site URL>"
$oWEb = Get-SPWeb $SiteURL
$PublishingComment ="Published using powershell"

Use below block if you wanted to publish a single page, replace with your page url.

<# Single page publish #>
$pageURL = "https://digiboek.net/sites/demo/SitePages/Sample.aspx"
$file = $oWEb.GetFile($pageURL)
$file.Publish($PublishingComment);

If you wanted to publish multiple pages then use below piece of code

<# Publish all pages from library #>
$oLib = $oWEb.GetFolder($LibraryTitle)
$files = $oLib.Files | select Name, Url
foreach ($file in $files) {
    $ofileURL = $SiteURL + "/" + $file.Url
    $oFile = $oWEb.GetFile($ofileURL)
    if ($oFile.Exists -eq $true) {
        $oFile.Publish($PublishingComment);
        Write-Host $file.Name" Page published successfully..." -foreground Green
    }
}

Happy Coding 😊