Search code examples
azureazure-powershellazure-data-lake-gen2

Extract ADLS gen2 details


How to get adls gen2 details. I want path along with size in kb recursively. I was tring with the below code but it is showing only size of all the folder not even shoing the path.

$ctx = New-AzStorageContext -StorageAccountName "******" -UseConnectedAccount

$Files = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem "******" -Path "a/b/c" -Recurse 

$Total = $Files | Measure-Object -Property Length -Sum
$Total | Select-Object @{Name="Path";E={$Path}},@{Name = "SizeInKB"; Expression={$_.Sum/1KB}} 

My desire output is

Path    SizeInKB
a/b/c           0
a/b/c/ef       10
..........  ......

Solution

  • I created an ADLS Gen2 storage account and created a file system like below:

    enter image description here

    Under the file system, I created directories, sub directories and uploaded test files:

    enter image description here

    To fetch the path and the size in kb, modify the PowerShell script like below:

    $ctx = New-AzStorageContext -StorageAccountName "xxx" -UseConnectedAccount  
    $Path = "testdirectory/subdirectory"
    
    $Files = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem "filesystem1" -Path $Path -Recurse
      
    $Total = $Files | Measure-Object -Property Length -Sum  
    $Total | Select-Object @{Name="Path";E={$Path}},@{Name = "SizeInKB"; Expression={$_.Sum/1KB}}
    

    enter image description here

    To fetch the path and the size in kb for multiple paths, try the below code:

    $ctx = New-AzStorageContext -StorageAccountName "xxx" -UseConnectedAccount  
    $Paths = @("testdirectory/subdirectory", "test/testsub1")
    
    foreach ($Path in $Paths) {  
    $Files = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem "filesystem1" -Path $Path -Recurse  
    
    $Total = $Files | Measure-Object -Property Length -Sum  
    $Total | Select-Object @{Name="Path";E={$Path}},@{Name = "SizeInKB"; Expression={$_.Sum/1KB}}  
    }
    

    enter image description here