Foreach loop output file

Copper Contributor

Hi all, 

 

Some expert help please... I am really happy with how a foreach loop is working as I would expect it but I can't find a solution to export the results to a text file. The code is below: 

 

# Get all the teams from tenant
$teamColl=Get-Team

# Loop through the teams
foreach($team in $teamColl)
{
Write-Host -ForegroundColor Magenta "Getting all the channels from Team: " $team.DisplayName

# Get the channels for Team
$channelColl= Get-TeamChannel -GroupId $team.GroupId #-MembershipType Private

# Get the team owners
$ownerColl= Get-TeamUser -GroupId $team.GroupId -Role Owner

#Loop through the Teams
foreach($channel in $channelColl)
{
Write-Host -ForegroundColor Yellow "ID: " $channel.Id " Display Name: " $channel.DisplayName " Desc: " $channel.Description " Membership Type: " $channel.MembershipType

#Loop through the owners
foreach($owner in $ownerColl)
{
Write-Host -ForegroundColor Red "User ID: " $owner.UserId " User: " $owner.User " Name: " $owner.Name
}
}
}

 

Please give me any assistance. Thank you. 

1 Reply

Hi
You will need to add a variable before the Foreach type Array
$MyResult=@()
and in the Foreach loop, add the result to this variable.
$MyResult+="The Thing you want to add"

And After the ForEach loop, you will need to Export the content as text or what ever you want using
Out-File or Export-CSV ...

 

 

https://powershellmagazine.com/2013/02/04/creating-powershell-custom-objects/