1

I'm trying to write a fuction that returns [string[]] from a powershell function. But it always returns a generic object intead...

function MyGetFiles {   
    [OutputType('string[]')]
    [CmdletBinding()]
    param([string]$filter)  
    
    $found = New-Object -TypeName System.Collections.Generic.List[System.String]
    $cwd = (Get-Location).Path
    
    [string[]]$fileEntries = [IO.Directory]::GetFiles($cwd);       
    
    foreach ($file in $fileEntries) {
        if ($file -match $filter) {
            #write-host "FILE:" $file
            $found.Add($file)
        }
    }
    
    $arr= [string[]]$found.ToArray()
    $arr.GetType() | out-host
    
    return $arr
}

$wlf = MyGetFiles "\.wlf`$"

$wlf.GetType() | out-host

$wlf.Count

Script Output:

True     True     String[]    System.Array

True     True     String      System.Object

Error : The property 'Count' cannot be found on this object. Verify that the property exists.
At C:\Users\xx\Desktop\script.ps1:1675 char:1

Also, does anybody know how to get out-host to be the default instead of always pipeling to out-host?

pico
  • 1,660
  • 4
  • 22
  • 52
  • In short: PowerShell by default *enumerates* collections that you output from a function (whether you output them implicitly or via a `return` statement), i.e. it streams (outputs) the collection elements _one by one_. To output a collection *as a whole*, use `, $collection` (sic) or, more explicitly but less efficiently, `Write-Output -NoEnumerate $collection`. See the linked duplicates for details. – mklement0 Oct 29 '21 at 20:13
  • As for your second question: `Out-Host` _is_ the default - you don't need to use it explicitly, unless you explicitly want to bypass the success output stream (pipeline). If this explanation isn't enough, please ask a _new_ question. – mklement0 Oct 29 '21 at 20:16
  • I didn't understand the questions that you use to mark mine as a dupicate... does that mean i just need to add a comma before my return? Example: "return , $arr" – pico Oct 29 '21 at 20:18
  • what if your out-host isn't the default? – pico Oct 29 '21 at 20:19
  • yes, `return , $arr` is correct. But note that, in general, `return $var` is just syntactic sugar for `$var; return` - you only need `return` to unconditionally exit the scope, not for producing output. – mklement0 Oct 29 '21 at 20:21
  • I don't understand the `out-host` follow-up question. What do you mean by default? If a command's output is neither captured, sent to another command, or redirected, it goes to the host by default. – mklement0 Oct 29 '21 at 20:22
  • Still returns string instead of string[] with the comma – pico Oct 29 '21 at 20:23
  • Try `(& { , [string[]] ('foo', 'bar') }).GetType()`, and you'll see that the technique works in principle. Omit the `, ` to see the difference. – mklement0 Oct 29 '21 at 20:24
  • Ok... it works... visual studio code always locks my files and causes powershell to pick up a cached copy of the scripts instead... – pico Oct 29 '21 at 20:31
  • I'm glad to hear it's working now, but that VS Code symptom you describe sounds awfully strange - VS Code normally doesn't block reading of files it has opened for editing. – mklement0 Oct 29 '21 at 20:59

0 Answers0