Friday, May 16, 2014

PowerShell script for listing folders in a location with their last Accessed date and size

Today I was asked to write a power shell function for listing the folders in a location with their last Accessed date and size. Thought of sharing the same so that someone else can make use of it.

I just wrote this as function so that it can reused easily. This function give the result in excel


Function Get-FolderSize
{
 Param(
         [Parameter(Mandatory=$true)]
         [string]$loactaion
        )#end param

# Adding to Excell Sheet

$strComputer = “.”
$Excel = New-Object -Com Excel.Application
$Excel.visible = $True

$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = “Folder”
$Sheet.Cells.Item(1,2) = “Last Accessed”
$Sheet.Cells.Item(1,3) = “size”

$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
$intRow = 2


$colItems = Get-ChildItem  $loactaion -recurse | Where-Object {$_.PSIsContainer  }

foreach ($i in $colItems)
{
     $kk = Get-ChildItem  $i.FullName -recurse | Measure-Object -property length -sum
     $Sheet.Cells.Item($intRow,1) = $i.FullName
     $Sheet.Cells.Item($intRow,2) = $i.LastAccessTime
     $Sheet.Cells.Item($intRow,3) = "{0:N2}" -f ($kk.Sum / 1MB) + "MB"
     $intRow = $intRow + 1
}

$WorkBook.EntireColumn.AutoFit()
}


Usage

Get-FolderSize "D:\yourfolder" 

No comments:

Post a Comment