# I am new, i have a problem when excute 2 comman, need help

**URL:** https://forums.powershell.org/t/i-am-new-i-have-a-problem-when-excute-2-comman-need-help/18673
**Category:** PowerShell Help
**Created:** [February 24, 2022, 7:45am UTC](https://forums.powershell.org/t/i-am-new-i-have-a-problem-when-excute-2-comman-need-help/18673 "2022-02-24T07:45:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Bhuyquoc](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@Bhuyquoc](https://forums.powershell.org/u/Bhuyquoc)
#### Post date: [February 24, 2022, 7:45am UTC](https://forums.powershell.org/t/i-am-new-i-have-a-problem-when-excute-2-comman-need-help/18673/1 "2022-02-24T07:45:52Z")

</div>

i have a problem when Add a user to a channel, the comand add-TeamUser works , but Add-teamchannelUser is not., with error code.  
Add-TeamChannelUser : Error occurred while executing Add-TeamChannelUser  
Code: NotFound  
Message: User is not found in the team.  
Pls help to clarify my script  
thanks

Add-TeamUser -GroupId $teamgroupid -User $UserName  
Add-TeamChannelUser -GroupId $teamgroupid -DisplayName “$ChannelName” -User $UserName

```auto
function Add-UserToChannel
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $UserName,
        [Parameter(Mandatory=$true)]
        [string] $ChannelName
        
    )
    Process{
        $i = 0
        foreach($teamgroupid in $($allteamgroups.Groupid))
        {
            $i++
            $teamname = Get-Team -GroupId $teamgroupid
            Write-Progress -Activity "Finding All channels from $($teamname.DisplayName)" -Status "$i out of $($allteamgroups.Count) completed"
            $allteamchannel = Get-TeamChannel -GroupId $teamgroupid
            foreach($channelN in $($allteamchannel.DisplayName))
            {
                if($ChannelName -like $channelN)
                {
                    $TeamgroupName = Get-Team -GroupId $teamgroupid
                    $checkchannelUserName = Get-TeamChannelUser -GroupId $teamgroupid -DisplayName $ChannelName
                    if($($checkchannelUserName.User) -contains $UserName){
                      Write-Host("User already in $ChannelName") -ForegroundColor Green 
                    }else {
                            $checkteamUser = Get-TeamUser -GroupId $teamgroupid
                            If($checkteamUser.User -contains $UserName){
                                Write-Host("Adding $UserName to $ChannelName") -ForegroundColor Green
                                Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName
                                #Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName -Role Owner #Promote Use become a owner of channel                          
                            }else{
                                 Write-Host("Addding "+$UserName+" To Team: $($TeamgroupName.DisplayName) and to channel: $ChannelName") -ForegroundColor Green
                                 Add-TeamUser -GroupId $teamgroupid -User $UserName
                                 Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName
                            }
                            #Write-Host("I found thats channel name is: "+$ChannelName+" in team "+$TeamgroupName.DisplayName+" with teams groupid is: "+$teamgroupid)
                            #Add-TeamChannelUser -GroupId $teamgroupid -DisplayName "$ChannelName" -User $UserName -Role Owner #Promote Use become a owner of channel                          
                          }

                }
            }
        }

    }
}

```

---

<div class="post-metadata">

### Author: ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)
#### Post date: [February 24, 2022, 1:47pm UTC](https://forums.powershell.org/t/i-am-new-i-have-a-problem-when-excute-2-comman-need-help/18673/2 "2022-02-24T13:47:13Z")

</div>

I suspect the problem is not so much with the script but with teams itself.  
I believe that the Teams-service is not given enough time to register the user in the Team before the next cmdlet tries to add the user to a channel in the Team.  
It may be worth trying to add a `Start-Sleep -Seconds 15` (or some other value) between `Add-TeamUser` and `Add-TeamChannelUser` to give Teams the chance to see the newly added user.

---

<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:18pm UTC](https://forums.powershell.org/t/i-am-new-i-have-a-problem-when-excute-2-comman-need-help/18673/3 "2024-05-16T20:18:13Z")

</div>


