Search This Blog

Tuesday, February 27, 2018

Get All Files From SharePointLib To Excel - PowerShell



if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "SharepointLibFileReport"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

$header = "$("SharePoint FileName") `t$("File KB Size") `t$("File MB Size") `t$("SharePoint FilePath") `t$("Count")"
$header | out-file -FilePath $logfile

# Get all files from drive
$dirfiles = gci -Recurse $localFolderPath | select  name,Attributes,FullName,length | sort parent
$web = Get-SPWeb http://win-2016
$list = $web.Lists["Documents"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery;
$spQuery.ViewAttributes = "Scope='RecursiveAll'";
$spQuery.Query = '<OrderBy><FieldRef Name="ID" Ascending="True" /></OrderBy>'
$spListItems = $list.GetItems($spQuery)
$count=0;
$suffix = "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
$index = 0
foreach ($item in $spListItems)
{
$count++;

#To Calculate Total Size(MB)
$fileKBSize =  [Math]::Round(($item.File.Length/1KB),1);
$fileMBSize =  [Math]::Round(($item.File.Length/1MB),1);
write-host "file Name(Migrated) :" $item.Name "KB Length :" $fileKBSize "MB Length :"$fileMBSize  -ForegroundColor Green
$header = "$($item.Name) `t$($fileKBSize) `t$($fileMBSize) `t$($web.Url+"/"+$item.Url) `t$($count)"
$header | out-file -FilePath $logfile  -append
}





No comments:

Post a Comment