title: Can't export csv in powershell, empty CSV file
body: <p>I'm trying to get what permissions files and folders have and export to a csv file. I can get the info to display on screen, but when I try to export it the resulting csv file is empty.</p>
  
  <p>The code:</p>
  
  <pre><code>    function Test-IsWritable(){
  &lt;#
      .Synopsis
          Command tests if a file is present and writable.
      .Description
          Command to test if a file is writeable. Returns true if file can be opened for write access.
      .Example
          Test-IsWritable -path $foo
          Test if file $foo is accesible for write access.
      .Example
          $bar | Test-IsWriteable
          Test if each file object in $bar is accesible for write access.
      .Parameter Path
          Psobject containing the path or object of the file to test for write access.
  #&gt;
      [CmdletBinding()]
      param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][psobject]$path)
  
      process{
          Write-Host "Test if file $path is writeable"
          if (Test-Path -Path $path -PathType Any){
              $target = Get-Item $path -Force
              try{    
                  $writestream = $target.Openwrite()  
                  $writestream.Close() | Out-Null         
                  Remove-Variable -Name writestream
                  Write-Host "File is writable" -ForegroundColor DarkGreen
                  Write-Output $true
                  }
              catch{              
                  Write-Host "File is not writable" -ForegroundColor DarkRed
                  Write-Output $false
                  }
              Remove-Variable -Name target
          }
          else{
              Write-Host "File $path does not exist or is a directory" -ForegroundColor Red
              Write-Output $false
          }
      }
  }
  
  write-host "WARNING: If checking deep folders (where the full path is longer than 248 characters) please " -foregroundcolor Yellow -NoNewline
  Write-Host "MAP THE DRIVE " -ForegroundColor Red -NoNewline
  Write-Host "in order to keep the names as short as possible" -ForegroundColor Yellow
  $basefolder = Read-Host -Prompt 'What is the folder or files you want to get permissions of?'
  
  write-host "WARNING: if permissions.csv already exists, it will be overwritten!" -foregroundcolor Yellow
  Write-Host 'Export results to CSV? (y/n): ' -ForegroundColor Magenta -NoNewline
  $export = Read-Host 
  
  if ($export -like "y")
      {
          Write-Host "Name the file (ex: permissions.csv): " -ForegroundColor Magenta -NoNewline
          $FileName = Read-Host
  
          $Outfile = “$PSScriptRoot\$FileName”
  
  
          write-host "Will write results to $PSScriptRoot\$FileName" -ForegroundColor Green
      }
  
  else
      {
          write-host "User did not type 'y', continuing" -ForegroundColor DarkYellow
      }
  
  
  
  $files = get-childitem $basefolder -recurse -File
  
  Write-Host $files
  Write-Host "=========================" -ForegroundColor Black
  
  #$subfiles = Get-ChildItem $folders -Recurse -File
  
  #Write-Host $folders
  #Write-Host "=========================" -ForegroundColor Black
  #Write-Host $subfiles
  
  
  $results = foreach($folder in $files) {
  
  
              New-Object psobject -Property @{
                  File = $folder;
                  Access = "$basefolder\$folder" | Test-IsWritable
              }
              Write-Host $folder
  
  }
  
  #$subresults = foreach($subfile in $subfiles) {
  
  
     #         New-Object psobject -Property @{
     #             File = $subfiles;
     #             Access = $subfile | Test-IsWritable;
     #         }
  
  #}
  
  Write-Host $results
  Write-Host "Finished combo loop, exporting..." -ForegroundColor Green
  
  $results | Export-Csv $Outfile -NoTypeInformation -Delimiter ";"
  
  Write-Host "Converting delimited CSV to Column Excel Spreadsheet"
  $outputXLSX = $PSScriptRoot + "\$Filename.xlsx"
  $excel = New-Object -ComObject excel.application 
  $workbook = $excel.Workbooks.Add(1)
  $worksheet = $workbook.worksheets.Item(1)
  $TxtConnector = ("TEXT;" + $Outfile)
  $Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
  $query = $worksheet.QueryTables.item($Connector.name)
  $query.TextFileOtherDelimiter = ';'
  $query.TextFileParseType  = 1
  $query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
  $query.AdjustColumnWidth = 1
  $query.Refresh()
  $query.Delete()
  $Workbook.SaveAs($outputXLSX,51)
  $excel.Quit()
  
  Remove-Item $Outfile
  Write-Host "See $PSScriptRoot\$Filename.xlsx for results" -ForegroundColor Green
  </code></pre>
  
  <p>UPDATE: Mostly working, strange output though:</p>
  
  <blockquote>
    <p>Z:\testfolder\file1.txt 
    Z:\testfolder\file1.txt
    Z:\testfolder\file1.txt 
    Z:\testfolder\file1.txt
    Z:\testfolder\file1.txt 
    Z:\testfolder\file1.txt
    Z:\testfolder\file1.txt 
    Z:\testfolder\file2.txt
    Z:\testfolder\file2.txt 
    Z:\testfolder\file2.txt
    Z:\testfolder\file2.txt 
    Z:\testfolder\file2.txt
    Z:\testfolder\file2.txt 
    Z:\testfolder\file2.txt
    Z:\testfolder\file3.rar 
    Z:\testfolder\file3.rar
    Z:\testfolder\file3.rar 
    Z:\testfolder\file3.rar
    Z:\testfolder\file3.rar 
    Z:\testfolder\file3.rar
    Z:\testfolder\file3.rar 
    The specified path, file name, or both are too
    long. The fully qualified file name must be less than 260 characters,
    and the directory name must be less than 248 characters.</p>
  </blockquote>
  
  <p>In the next column:</p>
  
  <blockquote>
    <p>FileAccess 
    FullControl 
    FullControl 
    FullControl 
    Modify, Synchronize
    ReadAndExecute, Synchronize 
    Modify, Synchronize 
    Modify, Synchronize
    FullControl 
    FullControl 
    FullControl 
    Modify, Synchronize
    ...
    The specified path, file name, or both are too long. The fully
    qualified file name must be less than 260 characters, and the
    directory name must be less than 248 characters.</p>
  </blockquote>
  
  <p>I'm not sure why it's showing multiple rows for the same file, I'd like to have 1 row per file with the true File Access.</p>