Friday, October 16, 2015

PowerShell To Get Document Libraries Information

Export Sorted CheckedOut Files Using PowerShell

Sometimes in SharePoint When we need to get all the documents from SharePoint Libraries including all the sites and subsides, its resource intensive job specially in big environments with huge list contents and documents. But we can achieve or get these type of information using PowerShell and can schedule script execution at time when minimum load is on the servers. Following script will get all the checkedout files with version and export into CSV with heading sorted as required.


$url = "http://portal.contoso.com"
$site = New-Object Microsoft.SharePoint.SPSite($url)
$web = $site.OpenWeb()


function GetCheckedOutFiles($web)

{
Write-Host "Started Processing Web: $($web.Url)"
        foreach ($list in ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
            
            Write-Host "Started Processing List: $($list.RootFolder.ServerRelativeUrl)"
                                
            foreach ($item in $list.Items) {
           
                $modifiedTime = $web.RegionalSettings.TimeZone
                if ($item.File.CheckOutStatus -ne "None") {
                    if (($list.CheckedOutFiles | where {$_.ListItemId -eq $item.ID}) -ne $null) { continue }
                    $data = @{
                        "Library" = $list.Title
                        "Folder Name"= $item.Url.Substring(0,$item.Url.LastIndexOf("/"))
                        "Title" = $item.File.Name
                        "Version" = $item["Version"]
                        "Created By"= $item.File.Author.Name                        
                        # Get Created Time date with local time zone
                        "Date Created"= $modifiedTime.UTCTolocalTime($item.File.TimeCreated)
                        "Modified By" = $item.File.ModifiedBy.Name                        
                        # Get Modified Time date with local time zone
                        "Date Modified" = $modifiedTime.UTCTolocalTime($item.File.TimelastModified)
                                                
                    }
                    New-Object PSObject -Property $data | Select "Library", "Folder Name", "Title", "Version", "Created By", "Date Created", "Modified By", "Date Modified”
                }
            }
        }
foreach($subWeb in $web.Webs)
{
GetCheckedOutFiles($subweb)
}
        $web.Dispose()
}



#GetCheckedOutFiles | Out-GridView

GetCheckedOutFiles($web) | Export-Csv -NoTypeInformation -Path D:\CheckedOutFiles.csv