How to not send file attachment if contents are empty in Powershell RRS feed

  • Question

  • I wrote some powershell code to export a file. What code should I write to NOT export the file if the file contents are empty?

    The end of the code is below:

    $extractFile = "C:\Users\testname + $(((get-date).ToUniversalTime()).ToString("yyyyMMddThhmmssZ")) + $filename
    $testsummary= $QueryBaseResults.Tables[2] | Select 'value1', 'value2', 'value3', 'value4', 'value5' -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors  | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 |Set-Content -Path $extractFile

    Thank you.

    Thursday, August 16, 2018 7:17 PM

All replies

  • if ((Get-Content C:\myfile.txt) -eq $null) {
      "File is empty"

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, August 16, 2018 7:37 PM
  • Much faster and easier:

    if((Get-Item '.\test.bat').Length){
         Write-Host 'File is not empty'
         Write-Host 'File is empty'


    • Proposed as answer by John Seerden Friday, August 17, 2018 9:31 PM
    Thursday, August 16, 2018 7:45 PM
  • I agree with jrv, however I would avoid using Write-Host in favor of Write-Output.
    Friday, August 17, 2018 9:26 PM