I am new to powershell and i am trying to get the output of foreach loop into a file

the foreach loop looks like:

```
foreach ($element in $ashost)  {get_alerts($element)}

```

i am getting a list of alerts in to the standard output 

so i have tried to write the for loop output to a file in the below way:

```
$output =
foreach ($element in $ashost)  {get_alerts($element)}|
$output | Out-File "C:\File.txt"

```

but the file is created empty 

note : the "get\_alerts" is a function to capture each alert
which i didn't specify a particular return value its just printing to the screen from the 
Write-Host inside the function