Search This Blog

Tuesday, February 27, 2018

Get All File And Folder Details to Excel - PowerShell

Step 1: Save GetAllFileAndFolderFromDrive.PS1 below script
Step 2: Change the file Path.
Step 3: Save .bat(After script below line)  and execute run with admin


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "ReportFilesFolder"

$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)
$header = "$("Drive FileName") `t$("Drive FileSize") `t$("Drive/File") `t$("Drive FilePath") "
$header | out-file -FilePath $logfile

$localFolderPath = "D:\TestingFolder"
$count=0
$dirfiles = gci -Recurse $localFolderPath | select  name,Attributes,FullName,length | sort parent
$fileCount=0
$dirCount=0
$dfilesize="";

function ProcessFolder {
        ForEach($dfile in $dirfiles)
            {

                  if ($dfile.Attributes -ne "Directory")
                    { 
                     $count++;    
                     # working in files
                     $dfilesize= DisplayInBytes(($dfile).length);
                     write-host "file : " $dfile.FullName,"file size: " $dfilesize ,$count
                     $header = "$($dfile.Name) `t$($dfilesize) `t$('File') `t$($dfile.FullName) "
                     $header | out-file -FilePath $logfile  -append
                  }
                  else{
                  $dirCount++;
                  #Directory
                  write-host "folder : " $dfile.FullName ,$dirCount
                  $header = "$($dfile.Name) `t$('') `t$('Directory') `t$($dfile.FullName) "
                  $header | out-file -FilePath $logfile  -append
                  }
                
            }
         write-host "Total file's count : " ,$count,"  Total folder's count : "$dirCount
}
function DisplayInBytes($num)
{
    $suffix = "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
    $index = 0
    while ($num -gt 1kb)
    {
        $num = $num / 1kb
        $index++
    }

    "{0:N} {1}" -f $num, $suffix[$index]
}
#Download root files
ProcessFolder('')
#Download files in folders


---------------------------------------
cd /d %~dp0

powershell -noexit -file "GetAllFileAndFolderFromDrive.PS1" "%CD%"
pause




No comments:

Post a Comment