Link to home
Start Free TrialLog in
Avatar of kuzum
kuzum

asked on

powershel for folder sizes

HI

Could you please share a powershell script that looks for a specifc folder   from given path such as  \\server\sharedfolder1  
\\server\\sharedfolder2 and reports me in GB in a excel sheet please.  

I do not want to be checking my shares manually and hence powershell would be good idea?
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

$dirlist=import-csv C:\Users\dustin\dirs.csv
$outfile = "C:\users\dustin\dirOut.txt"

echo "Name,Size" >> $outfile

ForEach ($path in $dirlist) {

$name=$path.name

$totalsize = (get-childitem $name -recurse -force | measure-object -property length -sum)

$name + "," + "{0:N2}" -f ($totalsize.sum / 1GB) + " GB" >> $outfile

}

Open in new window


This will give you an output of the files and their sizes.  The example source CSV is attached.  Output looks like

Name,Size
C:\Users\dustin\Desktop,12.28 GB
C:\Users\dustin\Documents,106.02 GB

Open in new window

dirs.csv
Avatar of kuzum
kuzum

ASKER

im receving error and it is indicating that it can not open the file "D:\myname\dirs.csv

I saved the shared path in excel format?

there is spaces in the shared path which is \\abc-nasfs\foldername    

folder name has spaced like Customer Services team where you see the spaces in the " customer services team"
Avatar of kuzum

ASKER

hi Dustin,  

I think I know what the issue is here if Im not wrong. actual share called Customer SErvices team which is a share and when I do right click on the folder and go to properties, size is shown as 0 bytes, however if I go into the shared folder and highlight all the subfolders than I can see the size of it?  IS this something we can reflect on the powershell? all it needs to do is check the subfolders of the given shared name and give me the total size?

thanks for your time
How big is the actual size (MB/GB)?  I tested here with some shares and I get sizes back.  You can try this with 4 decimal places to see if it's pulling MB:

$dirlist=import-csv C:\Users\Administrator\Documents\dirs.csv
$outfile = "C:\Users\Administrator\Documents\dirOut.txt"


echo "Name,Size" >> $outfile

ForEach ($path in $dirlist) {

$name=$path.name


    $totalsize = (get-childitem "$name" -recurse -force | measure-object -property length -sum)
    $name + "," + "{0:N4}" -f ($totalsize.sum / 1GB) + " GB" >> $outfile

}

Open in new window


My results:

Name,Size
\\localhost\share\Clients,0.0034 GB
\\localhost\share\Company,0.0000 GB
\\localhost\Share,0.0059 GB

Open in new window


But if I had 2 decimal places it was truncating them to 0.00
Avatar of kuzum

ASKER

this particular one is 433 GB
Avatar of kuzum

ASKER

it says it cannot open file D:\myname\dirs.csv
Avatar of kuzum

ASKER

I noticed I Can't use get-content to any of my drives? it is deniying to give me access for some strange resaons? I do have access and I think this is to do with powershell?

thanks
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kuzum

ASKER

thanks for your valuable time.

>>>>>>>>>>>For not being able to open the csv, are there any format errors inside the csv?  (i.e. is there a comma in any of the paths where the path isn't enclosed in quotes?)>>>>>>>>>

In csv I used  d:\shared\sharename    . I also used Set-ExecutionPolicy unrestricted too.

strange thing is that I can see anything when I used get-content. this should give me something? yes I am domain admin.
Avatar of kuzum

ASKER

I attached the error message.
error.txt
Are you running powershell as Admin?  (Right click and do PowerShell as Admin)
Avatar of kuzum

ASKER

yes and it is still  giving me access denied error
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial