Export all documents/Folders permissions to CSV using SharePoint PowerShell



$ver = $host | select version
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Get the Site collection
$Site= Get-SPSite http://SiteURL

$outputPath = "c:\Permission.csv"

Function GetMyFiles($Folder)
{

    Write-Host "+in : "$Folder.Name
    foreach($file in $Folder.Files)
    {
        Write-Host "'t" $file.Name
        Add-Content -Path $outputPath -Value  "                     File Name : $($file.Name)"
        # $file.Item.RoleAssignments -fore yellow
        Add-Content -Path $outputPath -Value "                              Permission for the list item"
        foreach ($role in $file.Item.RoleAssignments)
        {
           $users = $role.member.name
           Add-Content -Path $outputPath -Value  "                              $($users)"

           foreach ($roledef in $role.RoleDefinitionBindings)
           {
              #Write-Host $users ":" $roledef.name
              Add-Content -Path $outputPath -Value  "                                          $($roledef.name)"
           }
        }
    }

    #Loop through all subfolders and call the function recursively
    foreach ($SubFolder in $Folder.SubFolders)
    {
        if($SubFolder.Name -ne "Forms")
        {
            Write-Host "'t" -NoNewline
            Add-Content -Path $outputPath -Value  "           Folder Name : $($SubFolder.Name)"
            GetMyFiles($Subfolder)
        }
    }

}

    #Loop throuh all Sub Sites
    foreach($Web in $Site.AllWebs)
    {
        Write-Host "—————————————————–"
        Write-Host "Site Name: '$($web.Title)' at $($web.URL)"
        Add-Content -Path $outputPath -Value  "Website : $($web.Title)"

        foreach($list in $Web.Lists)
        {
            #Filter Doc Libs, Eliminate Hidden ones
            if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) )
            {
                Write-Host "——————————————-" "+" $List.RootFolder
                Add-Content -Path $outputPath -Value  "           Library : $($List.RootFolder)"
                GetMyFiles($List.RootFolder)
            }
        }
    }

Comments

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell