title: I want to be able to split the output of a directory and its subdirectory
body: <p>I want to be able to split the output of a directory and its sub directory with PowerShell. Currently this is my code:</p>
  
  <pre><code>Get-Childitem -path \\serverName\lettertext\Customer -Recurse -Include *.txt |
    Out-file c:\Letters\output.txt
  </code></pre>
  
  <p>This lists all the text content of the directory and subfolder.</p>
  
  <p>The full directory format is:</p>
  
  <pre>\\serverName\lettertext\Customer\Client Group\Client\Client Division</pre>
  
  <p>For this reseaon I want to out the result in this format:</p>
  
  <pre>Customer| CLient Group| CLient| CLient Division| Outputs.txt</pre>
  
  <p>Does anyone know how I can achieve that?</p>
  
  <p>I changed the code, this is what I changed it to but its not working:</p>
  
  <pre><code># Trying to pull a directory listing of customized letter text files
  $Testing = $false
  $DisplayElapsedTime = 0
  [DateTime] $StartDate = Get-Date
  
  $RootPath = "\\servername\LetterText" 
  
  $pattern = "*.txt"
  $Customer = ""
  #$FileName = 'C:\letters\' + $FileName
  if ($Testing -eq $false) {
      $x = Read-Host 'Customer Name'
      if ($x -ne $null) {$Customer = $x}
      $x = $null
      $x = Read-Host 'File Name (blank for all) ' 
      if ($x -ne "") {$pattern = $x}
  } else {
  }
  
  if ($Customer -eq "") {
      $RequestedCustomer = " All"
      $FileName = 'LetterTextFiles ' + $StartDate.Year.ToString() +
                  $StartDate.Month.ToString('00') +
                  $StartDate.Day.ToString('00') + '.ltr'
  } else {
      $RequestedCustomer = $Customer
      $FileName = 'LetterTextFiles ' + $StartDate.Year.ToString() +
                   $StartDate.Month.ToString('00') +
                   $StartDate.Day.ToString('00') + '--' + $Customer + '.ltr'
  }
  #$FileName = 'C:\util\' + $FileName
  Write-Host '' | Out-File -FilePath $FileName -Encoding Default
  
  "Requested Customer: "+ $RequestedCustomer &gt;&gt; $FileName 
  "Requested Files: " + $pattern &gt;&gt; $FileName 
  
  $Path = $RootPath + '\' +$Customer
  
  cls
  try {
      $Results = Get-ChildItem $Path -Recurse -Include:$pattern 
  
      if ($DisplayElapsedTime -eq -1) {
          [DateTime] $StartTime
          #'Listing Time:'
          $StartTime = Get-Date
      }
      $DirectoryName = ''
      foreach ($result in $Results) {
          if ($DirectoryName -ne $result.Directory.Name) {
              #" " &gt;&gt; $FileName 
              $y = ""
              $DirectoryName = $result.Directory.Name
              $FullName = $result.FullName.Replace($RootPath,'').Split('\')
              $level = $result.FullName.Replace($RootPath,'').Split('\')
              if ($result.DirectoryName -eq $RootPath) {
                  $y = 'COBRAPoint base letters:'
                  #Write-Host $y | Out-File -FilePath $FileName -Encoding Default 
                  " " &gt;&gt; $FileName 
                  $y &gt;&gt; $FileName 
              } else {
                  for ($x = 0; $x -lt $level.Length -1; $x++) {
                      switch ($x) {
                          1 {$y = 'Customer: '+ $level[$x]}
                          2 {$y = ' Client Group: '+ $level[$x]}
                          3 {$y = '  Client: '+ $level[$x]}
                          4 {$y = '   Client Division: '+ $level[$x]}
                      }
                      #Write-Host $y|out-file -FilePath $FileName -Encoding Default 
                      $y &gt;&gt; $FileName
                  }
              }
          }
          $y = "`t" + $result.Name
          #Write-Host $y | Out-File -FilePath $FileName -Encoding Default
          $y &gt;&gt; $FileName
          #break
      }
  } catch [System.Exception] {
      $y = "No Files Found"
      $y &gt;&gt; $FileName
  }
  
  #Out-File $FileName
  
  #$Output &gt; 'out.txt'
  $FileName
  'Done'
  
  if ($DisplayElapsedTime -eq -1) {
      'Total Time:'
      New-TimeSpan -Start $StartTime -End (Get-Date)
  }
  </code></pre>