Thursday 24 September 2015

Powershell script to send an email to document owners in SharePoint

I have got a situation where I have to delete/do clean up the unwanted documents which are lying in document libraries.

I have come up with a solution to send an email to all the document owners to clean their unwanted files which they have uploaded and if they don't then forcefully delete the documents which are uploaded before a specific timeline.

Here is the sample Poweshell script to acheive the above need.

.ps1 file

# Add SharePoint cmdlets reference
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

function enumerateWebParts($Url) {
   $ArrList = [System.Collections.ArrayList]@();
   $site = new-object Microsoft.SharePoint.SPSite $Url
   foreach($web in $site.AllWebs) {
      $listcount = ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]}).count;
      write-host $listcount;
      foreach ($list in ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
         foreach ($item in $list.Items ) {
            $string1 = $item.File.Url;
            $string2 = $item["Author"];
            $UserField = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$string2)
            $UserObj =  $UserField.User;
            $string3 = $UserObj.Email
            $ArrList.Add($string3)
         }
      }
      $web.Dispose()
   }
   $ArrList = $ArrList | select -uniq
   write-host "$ArrList"
   foreach($useremail in $ArrList) {
   $emailFrom = "test@gmail.com"
   $emailTo = $useremail
   $subject = "User Name"
   $body = "$useremail Please cross check your documents that are going to expire in the below link"
   $smtpServer = "pubsmtp.progress.com"
   $smtp = new-object Net.Mail.SmtpClient($smtpServer)
   write-host "$useremail"
}
}
$webAppUrl = Read-Host "Enter the web application URL for ex:tesst.com"
$csvpath = Read-Host "Enter the csv file path to create for ex: c:\test.csv"
enumerateWebParts($webAppUrl)

       
   

No comments:

Post a Comment