powershell new-team does not return object in time

New Contributor

Hello, i am having a problem using powershell to add new Teams. let me show you some example code:





foreach($item in $list){
        #some stuff happens above thats working...

        if ($autoteam -eq $null){
        $teamid = Get-team -DisplayName $autoteam
        if ($teamid -eq $null){
            $teamid = new-team -displayname $autoteam -owner "" -template "EDU_Class" |out-null
        #this is my workaround 
        while ($teamid -eq $null){
            sleep -Seconds 3
            $teamid = Get-team -DisplayName $autoteam 

        add-teamuser -groupid $teamid.groupid -user $upn |out-null





The Problem i cannot wrap my head around is as follows: when i use "new-team" in an interactive PS-Session it returns the groupid as it should (checked the manual).


When using it inside my script it returns nothing.

i found a workaround in which i let the script wait some time, and after aprox. 3-5 seconds the team is created and get-team returns what it should.


It seems to me that the new-team cmdlet only waits till finished when in interactive shell, could that be the problem? and if so, how do you guys do this?


2 Replies
best response confirmed by newboerg83 (New Contributor)
Hello, Isn't the out-null causing that?

@Andres Gorzelany 


OMG I'm feeling stupid, you're right. Its obvious.


Thanks Man.