Forum Discussion
BryanPAU
Mar 07, 2024Copper Contributor
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 ...
Mar 13, 2024
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/