title: Creation of Directory Azure Data Lake Gen 2 Automatically - PowerShell
body: <p>I want to create directories in the Azure Data Lake Gen2 in this format. YYYY = 2020 (Current Year) -&gt; MM = 10 (Current Month), DD = 28 (Current Date) and inside the Date folder I need to place the data file (Data.csv). i.e: /YYYY=2020/MM=10/DD=28/Data.csv.</p>
  <p><strong>Code Used for Data.csv in Root Folder Directory:</strong> <strong>With the Help of @Jim Xu</strong></p>
  <pre><code>$username = &quot;xyz@abc.com&quot;
  $password =ConvertTo-SecureString &quot;&quot; -AsPlainText -Force
  $credential = New-Object System.Management.Automation.PsCredential($username,$password)
  
  
  #Connect-AzureRmAccount -Credential $credential | out-null
  
  Connect-AzAccount -Credential $credential
  $dataFactoryName=&quot;&quot;
  $resourceGroupName=&quot;&quot;
  # get dataFactory triggers
  $triggers=Get-AzDataFactoryV2Trigger -DataFactoryName $dataFactoryName  -ResourceGroupName $resourceGroupName
  $datas=@()
  foreach ($trigger in $triggers) {
      # get the trigger run history
      $today = Get-Date
      $yesterday = $today.AddDays(-1)
       $splat = @{ 
          ResourceGroupName       = $trigger.ResourceGroupName
          DataFactoryName         = $trigger.DataFactoryName
          TriggerName             = $trigger.Name
          TriggerRunStartedAfter  = $yesterday
          TriggerRunStartedBefore = $today
     }
      
     $historys =Get-AzDataFactoryV2TriggerRun @splat
     if($historys -ne $null){
       # create date
       foreach($history in $historys){
          $obj =[PsCustomObject]@{
              'TriggerRunTimestamp '     = $history.TriggerRunTimestamp
              'ResourceGroupName '   =$history.ResourceGroupName
              'DataFactoryName' =$history.DataFactoryName
              'TriggerName '  = $history.TriggerName
              'TriggerRunId'= $history.TriggerRunId
              'TriggerType'=$history.TriggerType
              'Status' =$history.Status
  
          }
          # add data to an array
          $datas += $obj
       }
     } 
     
    
   }
   #  convert data to csv string
   $contents =(($datas | ConvertTo-Csv -NoTypeInformation) -join [Environment]::NewLine)
  
   # upload to Azure Data Lake Store Gen2
  
   #1. Create a sas token
   $accountName=&quot;testadls05&quot;
   $fileSystemName=&quot;test&quot;
   $filePath=&quot;data.csv&quot;
   $account = Get-AzStorageAccount -ResourceGroupName andywin7 -Name $accountName
   $sas= New-AzStorageAccountSASToken -Service Blob  -ResourceType Service,Container,Object `
        -Permission &quot;racwdlup&quot; -StartTime (Get-Date).AddMinutes(-10) `
        -ExpiryTime (Get-Date).AddHours(2) -Context $account.Context
  $baseUrl =&quot;https://{0}.dfs.core.windows.net/{1}/{2}{3}&quot; -f $accountName ,  $fileSystemName, $filePath, $sas
  #2. Create file
  $endpoint =$baseUrl +&quot;&amp;resource=file&quot;
  
  Invoke-RestMethod -Method Put -Uri $endpoint -Headers @{&quot;Content-Length&quot; = 0} -UseBasicParsing
  
  #3 append data
  $endpoint =$baseUrl +&quot;&amp;action=append&amp;position=0&quot;
  Invoke-RestMethod -Method Patch -Uri $endpoint -Headers @{&quot;Content-Length&quot; = $contents.Length} -Body $contents -UseBasicParsing
  
  #4 flush data
  $endpoint =$baseUrl + (&quot;&amp;action=flush&amp;position={0}&quot; -f $contents.Length)
  Invoke-RestMethod -Method Patch -Uri $endpoint -UseBasicParsing
  
  #Check the result (get data)
  
  Invoke-RestMethod -Method Get -Uri $baseUrl -UseBasicParsing
  </code></pre>
  <p>If anyone is having suggestions, please do post. Thanks</p>