# Function return values to CSV

**URL:** https://forums.powershell.org/t/function-return-values-to-csv/14492
**Category:** PowerShell Help
**Created:** [June 5, 2020, 1:46pm UTC](https://forums.powershell.org/t/function-return-values-to-csv/14492 "2020-06-05T13:46:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![wim](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wim/32/1168_2.png) [@wim](https://forums.powershell.org/u/wim)
#### Post date: [June 5, 2020, 1:46pm UTC](https://forums.powershell.org/t/function-return-values-to-csv/14492/1 "2020-06-05T13:46:01Z")

</div>

Hi

How do I get the return values from a function to a csv-file? Next code indicates what I want to do:

[pre]Function New-Array  
{  
$array = @()

for($i=0;$i -lt 50)  
{  
$array += some\_value  
}

return $array  
}

New-Array | Export-Csv -Path some\_path[/pre]

For the moment I don’t get the right values, but the number 10 on each row. The number of rows is correct.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [June 5, 2020, 2:29pm UTC](https://forums.powershell.org/t/function-return-values-to-csv/14492/2 "2020-06-05T14:29:46Z")

</div>

The proper way to create such a function would be this:

```
Function New-Array {
    $array = for ($i = 0; $i -lt 50; $i++) {
        $i
    }
    return $array
}
```

or even simpler like this:

```
Function New-Array {
    for ($i = 0; $i -lt 50; $i++) {
        $i
    }
}
```

But actually you don’t need a function to create an array of increasing numbers. This would work as well:

```
1..50
```

---

<div class="post-metadata">

### Author: ![wim](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wim/32/1168_2.png) [@wim](https://forums.powershell.org/u/wim)
#### Post date: [June 8, 2020, 1:20am UTC](https://forums.powershell.org/t/function-return-values-to-csv/14492/3 "2020-06-08T01:20:51Z")

</div>

Olaf, the real function creates passwords and set it to the users. I need to return the array to a csv for distributing the password to the new users.

I have adopted my function to your first example as suggested:

[pre]$array = for …[/pre]

Still I get the next return instead of the passwords:

[pre]#TYPE System.String  
“Length”  
“10”  
“10”  
“10”  
“10”  
“10”  
“10”[/pre]

---

<div class="post-metadata">

### Author: ![wim](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wim/32/1168_2.png) [@wim](https://forums.powershell.org/u/wim)
#### Post date: [June 8, 2020, 2:29am UTC](https://forums.powershell.org/t/function-return-values-to-csv/14492/4 "2020-06-08T02:29:38Z")

</div>

It’s solved. At the top of the function, I define my parameters and there stood the following:

[pre][Outputtype([int])[/pre]

I changed it to:

[pre][Outputtype([System.Object])][/pre]

Now it works.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/function-return-values-to-csv/14492/5 "2024-05-16T20:29:22Z")

</div>


