Wednesday, April 22, 2015

PowerShell to get list of SharePoint Folders Names , Files counts and File Name in the CSV format

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


$webUrl ="http://sp2010:23934/TEST" # Weburl till the Library
$listName = "TEST" # Document Library Name
$destination=".\"

$Datetime= Get-Date -Format dd_MM_yyyy_HH.mm.ss 
$LogFileName=([string]::Concat("DPN_LogFile " , $Datetime ,".csv"))
     New-Item  .\$LogFileName  -ItemType file 
        "variable initialization completed.."
        


$sc = New-Object Microsoft.SharePoint.SPSite($webURL)
$web = $sc.OpenWeb()
$list = $web.Lists[$listName]
$folderscount = 0
$folderswithresumes =0
$filesCount =0
$folderscount = $list.Folders.Count
Add-Content  .\$LogFileName -Value $webUrl
 Add-Content  .\$LogFileName -Value "Number of Folders : $folderscount"
 Add-Content .\$LogFileName -Value ([string]::Concat("GUID_Name" +","+ "Files_Count"+","+ "File_Name"))

 #Folder Name in each of the folder
foreach ($folder in $list.Folders) 
{
   $FileNames=""
   $foldFirstLvl = $list.ParentWeb.GetFolder($web.Url + "/" + $list.RootFolder.Url + "/" + $folder.Name)
   $filesCount = $foldFirstLvl.Files.Count
   
   #File Counts from each SP folder
   foreach ($file in $foldFirstLvl.Files) 
   {
 $FileNames = $FileNames + ","+ $file.Name
   }
   $FileNames = $FileNames.trim(",")
   Add-Content .\$LogFileName -Value ([string]::Concat($folder.Name +","+ $filesCount +","+ $FileNames))
   
   
}


$web.Dispose()

No comments:

Post a Comment

HTML

Script:

JS