Forum Discussion

MichaelNazli's avatar
MichaelNazli
Copper Contributor
May 30, 2020

Trying to create a table via Powershell

Hi Guys, I do have bunch of target folders that I'm trying to check them regularly via PS script;

 

RootFolder\AAAA0001\Apps\...
RootFolder\BBBB0001\Apps\...
RootFolder\CCC00001\Apps\...
RootFolder\DDDD0001\Apps\...
goes on

 

I wanna get an output something like this

Ref                  AppFolderSize(GB)
AAAA0001     3GB
BBBB0001      2GB
CCCC0001      5GB
DDDD0001    8GB
etc. 

 

Has someone done this? 

  •  

     

    dir c:\RootFolder -dir |
       ft name,@{Label=”AppFolderSize(GB)”;Expression= {“{0:n2} GB” -f ((dir -rec $_|measure length -sum -ErrorAction SilentlyContinue).sum /1gb)}}

     

    MichaelNazli  the script above is a single line of code and the output will be something like this:

     

    Name            AppFolderSize(GB)
    ----            -----------------
    kkk             0,00 GB          
    m               2,54 GB          
    office2019-last 1,88 GB          
    symbols         0,00 GB          
    up2             0,00 GB          
    uplanner        0,00 GB     

     

     

    bye Gas

  • gastone's avatar
    gastone
    Brass Contributor

     

     

    dir c:\RootFolder -dir |
       ft name,@{Label=”AppFolderSize(GB)”;Expression= {“{0:n2} GB” -f ((dir -rec $_|measure length -sum -ErrorAction SilentlyContinue).sum /1gb)}}

     

    MichaelNazli  the script above is a single line of code and the output will be something like this:

     

    Name            AppFolderSize(GB)
    ----            -----------------
    kkk             0,00 GB          
    m               2,54 GB          
    office2019-last 1,88 GB          
    symbols         0,00 GB          
    up2             0,00 GB          
    uplanner        0,00 GB     

     

     

    bye Gas

  • MichaelNazli I think this should help you. 

    $FoldersInRootFolder = Get-ChildItem -Path '\RootFolder\AAAA0001' -Directory -ErrorAction SilentlyContinue
    
    $Results = @()
    
    ForEach($Folder in $FoldersInRootFolder)
     {
      $Childs = Get-ChildItem -Path $Folder.FullName -Recurse -Force -ErrorAction SilentlyContinue
    
      $Sum = 0
       ForEach($Child in $Childs)
        {
         $Sum = $Child.Length + $Sum
        }
      
      $SumMB = $Sum / 1GB
    
      $Props = [ordered]@{
                          Ref   	          = $Folder.Name
                          "AppFolderSize(GB)" = $([math]::Round($SumMB,2))     
                         }
      $Results += New-Object PSObject -Property $Props
     }
    
    $Results

     

    Regards

    Erick Moreno

Resources