Wednesday, 20 May 2015

Get extract of files modified after particular date from SharePoint library or folder within library (Powershell script)

Function getFilesWithin($list)
{
    foreach($item in $list.Items)
    {
if ($folderURL -ne "")
{
if ($item.Url.StartsWith($folderURL, "CurrentCultureIgnoreCase"))
{
$targetFile = $item.file;
if ($targetFile.TimeLastModified -ge (Get-Date $lastDate)){
$item.Name + ";" + $item.Url + ";" + "File;" + $($targetFile.TimeLastModified).ToString('dd/MM/yyyy') | Out-File $logWrite -Append
} else {          
write-host -f green "Not changed after given date" + $item.Url
}
}
}
else
{
$targetFile = $item.file;
if ($targetFile.TimeLastModified -ge (Get-Date $lastDate)){
$item.Name + ";" + $item.Url + ";" + "File;" + $($targetFile.TimeLastModified).ToString('dd/MM/yyyy') | Out-File $logWrite -Append
} else {          
write-host -f green "Not changed after given date" + $item.Url
}
}
}
}

$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
if ($snapin -eq $null) {
    Write-Host "[INIT] Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

#****PARAMETRES DEFINITION****
#Specify web site URL wherein Document Library exists
$web= Get-SpWeb "http://w2k8sp2010dev01:13432/"
#Specify Title of Document Library
$DocLibName = "Shared Documents"
#Specify Folder URL if in case any specific documents within folder has to be worked upon or keep blank
$folderURL = ""
#Log File Name, will be suffixed by date time 
$LogFileName = "Get_LibFiles_ModifiedAfter_Log"
#Specify date after which files modified should be extracted
$lastDate = "2015-01-30"
#****PARAMETRES DEFINITION****

$LogFileName = $($LogFileName) + "@" + $(Get-Date -format MMddyy) + ".csv"
write-host -f RED $LogFileName

set-variable -option constant -name logWrite -value $($LogFileName)
"File Name;File URL;Type;Last Modified" | Out-File $logWrite -Append

write-host -f White "Connected to Web"
$Lib=$web.Lists.TryGetList($DocLibName)
write-host -f White "Connected to Library"

if ($Lib.LastItemModifiedDate -ge (Get-Date $lastDate)){
write-host -f yellow "Changed after given date " + $($Lib.LastItemModifiedDate)
} else {          
write-host -f green "Not changed after given date"
}

getFilesWithin($Lib)

$web.Dispose();

**************SCRIPT ENDS HERE***************

Instructions to use above Power-shell script;
You will need to change parameter values and below are the definition of parameters;

  1. Web Site URL - Specify web site URL 
  2. Title of Document Library - Specify Document Library Name
  3. Folder URL - Specify Folder URL starting with Library Name For E.g. "shared documents/folder name"
  4. Log File Name - Specify Log File Name
  5. Date after which files modified - Specify Date criteria (should be in format of “yyyy-mm-dd”)


No comments:

Post a Comment