Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Folder sizes using Powershell

Posted on 2014-11-18
10
Medium Priority
?
282 Views
Last Modified: 2014-11-27
Hi
 I am trying to get folder sizes of shared  folders that are located on network drives. However when l use the Powershell script attached. I only get the first  3 folder sizes within each shared folder that l run the script on. I would like  the script to include all the folders in the shared  folder. I would appreciate your assistance.
$a = Get-Date
$Year = $a.Year
$Month = $a.Month
$Day = $a.Day
$report = "c:\Scripts\Folder Size $Year$Month$Day.txt"

"Date: " + $a.ToShortDateString()  | Out-File -FilePath $report
"Time: " + $a.ToShortTimeString()  | Out-File -Append -FilePath $report


Out-File -Append -FilePath $report -InputObject "----------------------------------------------------------------" 
Out-File -Append -FilePath $report -InputObject "---                 Start of Script                          ---" 
Out-File -Append -FilePath $report -InputObject "----------------------------------------------------------------" 

Function RootScan
  {
  #List all root folders in the drive
  $folders = Get-ChildItem -Path $mainfolder | ? { $_.psiscontainer -and $_.FullName -notmatch $exclude}
  #$folders = Get-ChildItem -Path $mainfolder -exclude $exclude | ? { $_.psiscontainer -and $_.FullName}
  Out-File -Append -FilePath $report -InputObject "Start Root"
  ForEach ($folder in $folders)
  {
    $subFolderItems = (Get-ChildItem $folder.FullName -Recurse | Measure-Object -property length -sum)
    $folder.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB" | Out-File -Append -FilePath $report
  }
Out-File -Append -FilePath $report -InputObject "Finished Root"
}

Function SubScan
{
#List all Group folders in the GroupDrive
ForEach ($folderGroup in $Group)
{
  #Write-Host "This is FolderGroup: $folderGroup"
  $foldersGroup = Get-ChildItem -Path $foldergroup | ? { $_.psiscontainer -and $_.FullName}
  $SubNum = $SubNum + 1
  Out-File -Append -FilePath $report -InputObject "Start $SubNum"

    ForEach ($child in $foldersGroup)
    {
    #Write-Host "This is FoldersGroup: $child"
    $subFolderItems = (Get-ChildItem $child.FullName -Recurse | Measure-Object -property length -sum)
    $Child.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB" | Out-File -Append -FilePath $report
    }
  
  Out-File -Append -FilePath $report -InputObject "Finished $SubNum"
}}

$mainfolder = "M:\small"
$SubNum = 2
#Enter Directories Requiring Individual Subfolder Count
#$Group = "C:\Temp\"
#Enter Directories Excluded from Root Count
$exclude = "Users Data", "Test"
RootScan
#SubScan

#$mainfolder = "c:\Temp"
#$SubNum = 1
#Enter Directories Requiring Individual Subfolder Count
#$Group = "c:\Temp"
#Enter Directories Excluded from Root Count
#$exclude = "1"
#RootScan
#SubScan

Out-File -Append -FilePath $report -InputObject  "----------------------------------------------------------------"
Out-File -Append -FilePath $report -InputObject  "---                 End of Script                            ---"
Out-File -Append -FilePath $report -InputObject  "----------------------------------------------------------------"

$a = Get-Date
"Date: " + $a.ToShortDateString()  | Out-File -Append -FilePath $report
"Time: " + $a.ToShortTimeString()  | Out-File -Append -FilePath $report

#Get-ChildItem | Get-Member -MemberType Property

Open in new window

0
Comment
Question by:Phil Mapfumo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40450430
The script is working fine. Maybe there are some permission issues stopping the script from further processing each root folder?
0
 
LVL 1

Author Comment

by:Phil Mapfumo
ID: 40452078
All the permissons are similar on all the drives  and folders but the result is the same when l run the script on any of them l can only get the results for the first 3 folders. So doubt that can be the issue, however the drives are SAN drives  mapped to the server via ICSI and the permisson are inherited from the root folder
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 2000 total points
ID: 40452253
Try the script first with a local folder having more than three subfolders. I've done local and remote and it always returned more than 3 subfolders.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Phil Mapfumo
ID: 40452311
Hi Qlemo
 You could be right, I  have seen the results of one of the drives on the server  and it has listed all the folders for the root folder so that  means the sctipt is working, so l will re-examine the permissons on the drives that have failed.
0
 
LVL 1

Author Comment

by:Phil Mapfumo
ID: 40461854
Hi Qlemo
I have changed the permissions and it has worked, thanks. However l have a new question, l can understand if it is not possible to answer it and l will be happy to close the question. Is it possible to have the script run and scan 3 folders are the same time or am l asking for too much?
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 2000 total points
ID: 40462705
That would be some work, because that script is not written in a way easy to change to other requirements. E.g. the logging is done both outside and inside of the function, it uses global vars instead of parameters etc. pp.
I've removed anything unnecessary, and improved the function call so you can use it multiple times:
$report = "c:\Temp\EE\Folder Size $(get-date -format "yyyyMMdd").txt"

@"
----------------------------------------------------------------
---     Start of Script: $(get-date)
----------------------------------------------------------------

"@ | Out-File -Append -FilePath $report

Function global:RootScan($mainfolder, $exclude)
{
  #List all root folders in the drive
  $folders = Get-ChildItem -Path $mainfolder | ? { $_.psiscontainer -and $_.FullName -notmatch $exclude}
  Write-Output "-- Start Root of $mainfolder"
  ForEach ($folder in $folders)
  {
    $subFolderItems = (Get-ChildItem $folder.FullName -Recurse | Measure-Object -property length -sum).Sum
    Write-Output ($folder.FullName + " -- " + "{0:N2}" -f ($subFolderItems / 1MB) + " MB")
  }
  Write-Output "-- Finished Root of $mainfolder"
}

Start-Job -ScriptBlock (get-command RootScan).ScriptBlock -ArgumentList "M:\Small"       , @("Users Data", "Test") | Out-Null
Start-Job -ScriptBlock (get-command RootScan).ScriptBlock -ArgumentList "N:\Even Smaller", @("User Profiles")      | Out-Null
# and so on

while ($job = Get-Job | Wait-Job -Any)
{
  Receive-Job $job | Out-File -Append $report
  Remove-Job $job
}

@"
----------------------------------------------------------------
---                 End of Script: $(get-date)
----------------------------------------------------------------
"@ | Out-File -Append $report

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40462708
Though, it would be much better to generate objects instead of strings, and export as CSV ...
0
 
LVL 1

Author Comment

by:Phil Mapfumo
ID: 40468757
Great thank for your help Qlemo much appreciated!
0
 
LVL 1

Author Closing Comment

by:Phil Mapfumo
ID: 40468765
The expert was able to provide an additional solution that was not part of the original question, great guy!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question