How to return an empty array from a function

Welcome Forums General PowerShell Q&A How to return an empty array from a function

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
2 weeks, 1 day ago.

  • Author
    Posts
  • #113257

    Participant
    Points: 0
    Rank: Member

    How do you get back an empty array from a function? Unless you wrap the returned value in the array initialisation syntax @(), you allways seem to get a null see worked examples below:

    function Invoke-TestZero(){
        Get-Process
    }
    
    function Invoke-TestOne(){
        Get-Process | ? { $_.Name -eq 'xxxDoesNotExistxxx' }
    }
    
    function Invoke-TestTwo(){
        @(Get-Process | ? { $_.Name -eq 'xxxDoesNotExistxxx' })
    }
    
    
    function  Invoke-TestThree
    {
        [CmdletBinding()]
        [OutputType([System.Object[]])]
        Param ( )
    
        End
        {
            @(Get-Process | ? { $_.Name -eq 'xxxDoesNotExistxxx' })
        }
    }
    
    try{
        $x = Invoke-TestZero
        $x.GetType()
        write-host "Test Zero Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test Zero failed`r`n" -ForegroundColor Red }
    
    try{
        $x = Invoke-TestOne
        $x.GetType()
        write-host "Test One Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test One failed`r`n" -ForegroundColor Red }
    
    try{
        $x = Invoke-TestTwo
        $x.GetType()
        write-host "Test Two Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test two failed`r`n" -ForegroundColor Red }
    
    try{
        $x = Invoke-TestOne
        $x.GetType()
        write-host "Test Three Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test three failed`r`n" -ForegroundColor Red }
    
    try{
        $x = Invoke-TestTwo
        $x.GetType()
        write-host "Test Four Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test Four failed`r`n" -ForegroundColor Red }
    
    try{
        $x = @(Invoke-TestOne)
        $x.GetType()
        write-host "Test five Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test Five failed`r`n" -ForegroundColor Red }
    
    try{
        $x = @(Invoke-TestTwo)
        $x.GetType()
        write-host "Test Six Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test Six failed`r`n" -ForegroundColor Red }
    
    try{
        $x = Invoke-TestThree
        $x.GetType()
        write-host "Test Seven Suceeded`r`n" -ForegroundColor Green
    } catch { write-host "Test Seven failed`r`n" -ForegroundColor Red }
    
    

     

  • #113353

    Keymaster
    Points: 1
    Rank: Member

    write-output @() would return an empty array.

  • #113360

    Participant
    Points: 1
    Rank: Member

    Hmm, no, it wouldn't. Write-Output enumerates collections, sending items one at a time... and there's nothing in that collection.

    In order to do this, you'd need to make use of the unary comma operator:

    ,@()

    That said, I'm not sure why you'd do such a thing. An empty array is a useless object — any operations to add another item will destroy the original array and create another from scratch, so it makes no sense to create or return an empty array. If you want this for the purposes of returning a collection you can add things to and reference, you can instead do this:

    ,[System.Collections.Generic.List[object]]::new()
    • #113363

      Participant
      Points: 0
      Rank: Member

      Thanks, but you can assert if there are any items in an empty array.
      A reason for this might look like the following

      # should be able to return an empty collection
      $exclusionList = Get-ExclusionList
      
      $list = get-List
      
      # return items not in the exclusion list
      $list | % {
        if($exclusionList -notcontains $_){
          $x
        }
      }
      

You must be logged in to reply to this topic.