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?