Welcome Forums General PowerShell Q&A moving command into function question

Viewing 1 reply thread
  • Author
    Posts
    • #166196
      Participant
      Topics: 7
      Replies: 7
      Points: 12
      Rank: Member

      hello all,

      while attempting to count files of a particular directory by running directly a command (shown below) my output is the expected one. However, when I’m wrapping that command into a function (or a variable) and then calling it from an if statement the output is not correct.

      In specific, my Directory has 4 files at the moment.

      [C:\Temp]> ls
      
      Directory: C:\Temp
      
      Mode LastWriteTime Length Name
      ---- ------------- ------ ----
      d----- 16/07/2019 14:04 PM New folder
      d----- 16/07/2019 14:04 PM New folder - Copy
      -a---- 16/07/2019 14:04 PM 0 New Microsoft Word Document - Copy.docx
      -a---- 16/07/2019 14:04 PM 0 New Microsoft Word Document.docx

      Creating a function to measure dir contents:

      [C:\Temp]> function Get-DirContents { (([System.IO.Directory]::EnumerateFileSystemEntries("C:\Temp\")) | Measure-Object).Count }

      Which seems to count dir contents

      [C:\Temp]> Get-DirContents
      4

      and if I delete the files bring zero:

      [C:\Temp]> Get-DirContents
      0

      when I’m adding that function to an if statement the result is not correct:

      [C:\Temp]> if (Get-DirContents -eq '0') {Write-Host "dir is empty"} else {Write-Host "files copied"}
      dir is empty
      
      [C:\Temp]> Get-DirContents
      4
      
      [C:\Temp]> if (Get-DirContents -eq '0') {Write-Host "dir is empty"} else {Write-Host "files copied"}
      dir is empty
      
      

      but this way works:

      if ((([System.IO.Directory]::EnumerateFileSystemEntries("C:\Temp\")) | Measure-Object).Count -eq '0') {Write-Host "Dir is empty"} else { Write-Host "files copied"}
      files uploaded

      I would like to understand why if I wrap the command into a function is not working and if use the command works.

    • #166277
      Participant
      Topics: 1
      Replies: 63
      Points: 355
      Helping Hand
      Rank: Contributor

      What I find interesting, is that whatever the first -eq is set to always evaluates as true.

      PS C:\temp> if (Get-DirContents -eq 1) {get-dircontents} else {Write-Host "files copied"}
      5
      PS C:\temp> if (Get-DirContents -eq '0') {Write-Host "dir is empty"} else {Write-Host "files copied"}
      dir is empty
      

      However, I ended up doing this

      $count=Get-DirContents
      $count
      5
      PS C:\temp> if ($count -eq 0){Write-Host "dir is empty"} else {Write-Host "files copied"}
      files copied
      PS C:\temp> if ($count -eq 5){Write-Host "dir is empty"} else {Write-Host "files copied"}
      dir is empty
      

      So, I find it odd that if I put it into a variable it works.
      Finally I did this:

      function get-dircontents {(get-childitem -path C:\temp *).count}
      PS C:\temp> get-dircontents
      5
      PS C:\> if (get-dircontents -eq 5) {write-host True} else {write-host false}
      True
      PS C:\> if (get-dircontents -eq 0) {write-host True} else {write-host false}
      True
      

      Which led me to this link:
      https://stackoverflow.com/questions/18148560/powershell-functions-that-return-true-false

      • #166316
        Participant
        Topics: 1
        Replies: 63
        Points: 355
        Helping Hand
        Rank: Contributor

        Replying to myself because I couldn’t just leave well enough alone…

        Going from the previous result:

         

        $count + 5
        10
        
        get-dircontents + 5
        5
        
        (get-dircontents) + 5
        10
        PS C:\temp> if (Get-DirContents -eq '0') {Write-Host "dir is empty"} else {Write-Host "files copied"}
        dir is empty
        PS C:\temp> if ((Get-DirContents) -eq '0') {Write-Host "dir is empty"} else {Write-Host "files copied"}
        files copied
        

        So wrapping the function in () allows it to return as a number….

Viewing 1 reply thread
  • The topic ‘moving command into function question’ is closed to new replies.