Foreach loop output file

%3CLINGO-SUB%20id%3D%22lingo-sub-2460725%22%20slang%3D%22en-US%22%3EForeach%20loop%20output%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2460725%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESome%20expert%20help%20please...%20I%20am%20really%20happy%20with%20how%20a%20foreach%20loop%20is%20working%20as%20I%20would%20expect%20it%20but%20I%20can't%20find%20a%20solution%20to%20export%20the%20results%20to%20a%20text%20file.%20The%20code%20is%20below%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Get%20all%20the%20teams%20from%20tenant%3CBR%20%2F%3E%24teamColl%3DGet-Team%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Loop%20through%20the%20teams%3CBR%20%2F%3Eforeach(%24team%20in%20%24teamColl)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Host%20-ForegroundColor%20Magenta%20%22Getting%20all%20the%20channels%20from%20Team%3A%20%22%20%24team.DisplayName%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Get%20the%20channels%20for%20Team%3CBR%20%2F%3E%24channelColl%3D%20Get-TeamChannel%20-GroupId%20%24team.GroupId%20%23-MembershipType%20Private%3C%2FP%3E%3CP%3E%23%20Get%20the%20team%20owners%3CBR%20%2F%3E%24ownerColl%3D%20Get-TeamUser%20-GroupId%20%24team.GroupId%20-Role%20Owner%3CBR%20%2F%3E%3CBR%20%2F%3E%23Loop%20through%20the%20Teams%3CBR%20%2F%3Eforeach(%24channel%20in%20%24channelColl)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Host%20-ForegroundColor%20Yellow%20%22ID%3A%20%22%20%24channel.Id%20%22%20Display%20Name%3A%20%22%20%24channel.DisplayName%20%22%20Desc%3A%20%22%20%24channel.Description%20%22%20Membership%20Type%3A%20%22%20%24channel.MembershipType%3CBR%20%2F%3E%3CBR%20%2F%3E%23Loop%20through%20the%20owners%3CBR%20%2F%3Eforeach(%24owner%20in%20%24ownerColl)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Host%20-ForegroundColor%20Red%20%22User%20ID%3A%20%22%20%24owner.UserId%20%22%20User%3A%20%22%20%24owner.User%20%22%20Name%3A%20%22%20%24owner.Name%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20give%20me%20any%20assistance.%20Thank%20you.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2460725%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2464793%22%20slang%3D%22en-US%22%3ERe%3A%20Foreach%20loop%20output%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2464793%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3CBR%20%2F%3EYou%20will%20need%20to%20add%20a%20variable%20before%20the%20Foreach%20type%20Array%3CBR%20%2F%3E%24MyResult%3D%40()%3CBR%20%2F%3Eand%20in%20the%20Foreach%20loop%2C%20add%20the%20result%20to%20this%20variable.%3CBR%20%2F%3E%24MyResult%2B%3D%22The%20Thing%20you%20want%20to%20add%22%3CBR%20%2F%3E%3CBR%20%2F%3EAnd%20After%20the%20ForEach%20loop%2C%20you%20will%20need%20to%20Export%20the%20content%20as%20text%20or%20what%20ever%20you%20want%20using%3CBR%20%2F%3EOut-File%20or%20Export-CSV%20...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fpowershellmagazine.com%2F2013%2F02%2F04%2Fcreating-powershell-custom-objects%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpowershellmagazine.com%2F2013%2F02%2F04%2Fcreating-powershell-custom-objects%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

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/