Thursday, August 29, 2019

How to find the size of a folder using PowerShell?



You can use the  Get-ChildItem to get the list of files and directories.

Mode                LastWriteTime         Length Name                                  
----                -------------         ------ ----                                  
d-----       2019-08-19  10:42 AM                APPDATA output                        
d-----       2019-08-12   3:18 PM                AWS                                   
-a----       2019-08-16  12:35 PM          15806 AppDataSize.csv                       
-a----       2019-08-28   3:39 PM          15358 Appwiz.txt                            


The Length object gives the size of the file, however that to get the size of the folder, we can use the  -Recurse to dive deep into the folder to get the details

PowerShell Code: 
Get-ChildItem -Path 'C:\Temp\APPDATA output' 

Mode                LastWriteTime         Length Name                                  
----                -------------         ------ ----                                  
-a----       2019-08-16  12:35 PM          15806 AppDataSize.csv                        
-a----       2019-08-16   1:12 PM           3909 AppDataSize2.csv                      
-a----       2019-08-16   3:33 PM           2744 AppDataSize2_User2.csv                
-a----       2019-08-16   5:18 PM          10737 AppDataSize_User.csv                   
-a----       2019-08-15  10:42 AM           2745 finalappdata.ps1                      
-a----       2019-08-16   1:12 PM          34616 output.txt 

PowerShell Code:
Get-ChildItem -Path 'C:\Temp\APPDATA output'| Measure-Object -Property Length -sum

Count    : 6
Average  :
Sum      : 70557
Maximum  :
Minimum  :
Property : Length


PowerShell Code: 
#Get the folder size and store it in the $Folder Variable
$Folder=Get-ChildItem -Path 'C:\Temp\APPDATA output'| Measure-Object -Property Length -sum | Select sum
#Converting them to a decimal number with the Size in MB
$FolderSize="{0:N2}" -f ($Folder.Sum / 1MB) + " MB"
Echo "Folder Size = $FolderSize"

Folder Size = 0.07 MB



To get the size of a folder in remote location we can use the same command and give the path of the location

PowerShell Code: 
#Get the folder size and store it in the $Folder Variable
$Folder=Get-ChildItem -Path '\\server1\Temp\APPDATA output'| Measure-Object -Property Length -sum | Select sum
#Converting them to a decimal number with the Size in MB
$FolderSize="{0:N2}" -f ($Folder.Sum / 1MB) + " MB"
Echo "Folder Size = $FolderSize"





No comments:

Post a Comment

How to suspend an script activity Temporarily ?

There are situations where the script execution needs to be suspended or paused for few milliseconds or seconds. PowerShell Syntax St...