Forum Discussion

BryanPAU's avatar
BryanPAU
Copper Contributor
Mar 07, 2024

error creating Sharepoint and Teams site via PS.

Hi,

 

I'm trying to create a script that will create a SharePoint site and then connected the relevant Teams with members. I would also like to create 3 private channels and an owner for them, but I can't seem to get over my current hurdle.  Anyone had a similar problem. I have run the install and update multiple times. 

 

Error Message: 

Write-Error: Error creating team site: The term 'New-PnPTeamsTeamSite' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

 

PowerShell: 

 

# Define parameters
$siteName = "Client - Test SP0200"
$groupOwner = "email address removed for privacy reasons"
$privacySettings = "Public"
$timeZone = "UTC+10:00 Sydney"
$siteUrl = "https://mycompany.sharepoint.com/sites/client-testsp0200"  # Reuse the site URL

# Connect to SharePoint Online interactively (supports MFA)
try {
    Connect-PnPOnline -Url $siteUrl -Interactive -ErrorAction Stop
}
catch {
    Write-Error "Error connecting to SharePoint: $_"
    exit
}

# Create a new team site
try {
    $team = New-PnPTeamsTeamSite -DisplayName $siteName -Alias $siteName -IsPublic $true -TimeZoneId $timeZone
}
catch {
    Write-Error "Error creating team site: $_"
    exit
}

# Set group owner
try {
    Set-PnPTeamsTeamSite -DisplayName $siteName -Owners $groupOwner
}
catch {
    Write-Error "Error setting group owner: $_"
    exit
}

# Wait for site creation to complete (optional, but recommended)
Start-Sleep -Seconds 30  # Adjust the wait time as needed

# Set privacy settings
Set-PnPTeamsTeamSite -DisplayName $siteName -IsPublic ($privacySettings -eq "Public")

# Add a Microsoft 365 group to the site
Add-PnPMicrosoft365GroupToSite -SiteUrl $siteUrl -DisplayName $siteName

# Enable a Microsoft Team for the site-connected group
New-PnPTeamsTeam -DisplayName $siteName -Alias $siteName -IsFavoriteByDefault $true

# Update team settings (if needed)
Set-Team -GroupId $team.GroupId `
    -AllowCreateUpdateChannels $false `
    -AllowDeleteChannels $false `
    -AllowAddRemoveApps $false `
    -AllowCreateUpdateRemoveTabs $false `
    -AllowCreateUpdateRemoveConnectors $false `
    -AllowCreatePrivateChannels $false

# Add members to the Team (if needed)
Add-TeamUser -GroupId $team.GroupId `
    -User email address removed for privacy reasons -Role Owner
Add-TeamUser -GroupId $team.GroupId `
    -User email address removed for privacy reasons -Role Owner

 

  • BryanPAU 

     

    I hope the following script will fulfill your needs: 

     

    #1.Script to create a teams(site) and channels:
    
    #Note : When you create a new team, the corresponding team site will created automatically.
     
    #Steps:
    #1."Install-Module PnP.Powershell" if it is not installed
    #2.Open the Windows Powershell ISE as run as administrator 
    #3.Run the script
     
    Connect-PnPOnline -Url "https://contoso-admin.sharepoint.com"
     
    $teamName="Enter the teamName"
    $teamDescription ="Enter the Team Description"
    $teamOwner ="email address removed for privacy reasons"
    $teamMember ="email address removed for privacy reasons"
    $chanelOwner = "email address removed for privacy reasons"
    $chanelNames ='TestPrivate channel1', 'TestPrivate channel2', 'TestPrivate channel3'
    try{
    #Create Team 
    $teamDetail= New-PnPTeamsTeam -DisplayName $teamName -Description $teamDescription -Owners $teamOwner -Visibility Public
    Start-Sleep -Seconds 5
    $teamId= $teamDetail.GroupId
    # Add members to the Team (if needed)
    Add-PnPTeamsUser -Team $teamId -User $teamMember -Role Member
    #Create Channel
    foreach($chnlName in $chanelNames)
    {
    Start-Sleep -Seconds 5
    Add-PnPTeamsChannel -Team $teamId -DisplayName $chnlName -OwnerUPN $chanelOwner -Private
    }
    }
    catch
    {
    Write-host -f red "Encountered Error:"$_.Exception.Message
    }
    
    #2.Enable a team and create a channels in existing group's site
    
    #Note : If you want to enable team and create channels in existing group's site .Then run the below script:
     
    $teamMemebrs="email address removed for privacy reasons","email address removed for privacy reasons"
    $channelNames = 'TestPrivate channel1', 'TestPrivate channel2', 'TestPrivate channel3'
    $channelOwner = "email address removed for privacy reasons"
    try{
    $siteUrl = "https://contoso.sharepoint.com/sites/sitename"
    $con=Connect-PnPOnline -Url $siteUrl
    $SiteInfo = Get-PnPTenantSite -Identity $siteUrl
    $GroupId = $SiteInfo.GroupId.Guid
    Start-Sleep -Seconds 5
    #Enable Teams in the group which is connected with the site
    New-PnPTeamsTeam -GroupId $GroupId 
    #Add members to Team 
    foreach($memberUPN in $teamMemebrs)
    {
    Add-PnPTeamsUser -Team $GroupId -User $memberUPN -Role Member
    }
    Start-Sleep -Seconds 5
    #Note:Channel Owner should be member/owner in teams
    foreach($chName in $channelNames)
    {
    Add-PnPTeamsChannel -Team $GroupId -DisplayName $chName -OwnerUPN $channelOwner -Private
    }
    }
    catch
    {
    Write-host -f red "Encountered Error:"$_.Exception.Message
    }

     

    If this solution proves helpful and resolves your issue, kindly consider marking it as accepted. Doing so will aid others who encounter similar challenges in the future.

    Regards,
    NarasimaPerumal Chandramoha
    Microsoft MVP
    Apps4.Pro - Trusted Office365 T2T Migration partner. Migrate Exchange, SharePoint, Microsoft Teams, Stream, Yammer, Power Platform between tenants.
    Product Specialist - http://www.apps4.pro/

    • BryanPAU's avatar
      BryanPAU
      Copper Contributor
      That does work but does not create the team SharePoint integration and clean as you would do it manually. url and email address are system generated etc.

Resources