Powershell scripts for creating subsite in sharepoint online

%3CLINGO-SUB%20id%3D%22lingo-sub-916006%22%20slang%3D%22en-US%22%3EPowershell%20scripts%20for%20creating%20subsite%20in%20sharepoint%20online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-916006%22%20slang%3D%22en-US%22%3EHi%2C%20anyone%20please%20help%20out%20with%20cmdlets%20to%20create%20a%20subsite%20in%20sharepoint%20online%20using%20sharepoint%20online%20management%20tool.%20Thanks%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-916006%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional Contributor
Hi, anyone please help out with cmdlets to create a subsite in sharepoint online using sharepoint online management tool. Thanks
10 Replies
Highlighted

@Dahmeey The code for creating sub site is

 

$username = "vsamal@myDomain.onmicrosoft.com"
$password = "MyPassword"
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
Connect-PnPOnline https://myDomain.sharepoint.com/sites/demo/ -cred $cred
New-PnPWeb -Title "SubSite A" -Url SubSiteA -Description "Information about SUBSITE A" -Locale 1033 -Template "STS#0"

 

Then the output comes like this:

2019-10-15_1322.png

Hope this helps!

Highlighted
Thanks for your response @Vikram_Samal please will this script run in sharepoint online management tool?
Highlighted

@Dahmeey Yes I tested there and it worked

Highlighted
@Vikram_Samal Would try it at work tomorrow and revert. Thanks alot
Highlighted
Hi @Vikram_Samal apologies for disturbing. May I know if you by any chance have the script to disable inheritance using the sharepoint management online tool?
Highlighted

Please find the break inheritance and permission assign code

 

 

#Create Subsite with Unique Permission and assign permisison to it 
cls
$username = "myusername@mydomain.onmicrosoft.com"
$password = "mypassword"
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
Connect-PnPOnline <a href="https://mydomain.sharepoint.com/sites/demo/" target="_blank">https://mydomain.sharepoint.com/sites/demo/</a> -cred $cred
New-PnPWeb -Title "SubSite B" -Url SubSiteB -Description "Information about SUBSITE B" -Locale 1033 -Template "STS#0" -BreakInheritance 
$owner = (Get-PnPContext).Credentials.UserName
#The owner will be the person provisioning the groups

#Create default groups for the new web and assign permisison
$ownerGroup = New-PnPGroup -Title "SubSite B Owners" -Owner $owner
Set-PnPWebPermission -Url SubSiteB -Group $ownerGroup -AddRole 'Full Control' 

$memberGroup = New-PnPGroup -Title "SubSite B Members"  -Owner $owner
Set-PnPWebPermission -Url SubSiteB -Group $memberGroup -AddRole Contribute

$visitorGroup = New-PnPGroup -Title "SubSite B Visitors" -Owner $owner
Set-PnPWebPermission -Url SubSiteB -Group $visitorGroup -AddRole Read

 

 

 

Hope this helps!

Highlighted

@Dahmeey Please find the code:

 

#Create Subsite with Unique Permission and assign permisison to it
cls
$username = "myusername@mydomain.onmicrosoft.com"
$password = "mypassword"
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
Connect-PnPOnline https://mydomain.sharepoint.com/sites/demo/ -cred $cred
New-PnPWeb -Title "SubSite B" -Url SubSiteB -Description "Information about SUBSITE B" -Locale 1033 -Template "STS#0" -BreakInheritance
$owner = (Get-PnPContext).Credentials.UserName
#The owner will be the person provisioning the groups

#Create default groups for the new web and assign permisison
$ownerGroup = New-PnPGroup -Title "SubSite B Owners" -Owner $owner
Set-PnPWebPermission -Url SubSiteB -Group $ownerGroup -AddRole 'Full Control'

$memberGroup = New-PnPGroup -Title "SubSite B Members" -Owner $owner
Set-PnPWebPermission -Url SubSiteB -Group $memberGroup -AddRole Contribute

$visitorGroup = New-PnPGroup -Title "SubSite B Visitors" -Owner $owner
Set-PnPWebPermission -Url SubSiteB -Group $visitorGroup -AddRole Read

 

 

Hope this helps!

Highlighted
Hi @Vikram_Samal below is the error I'm getting. kindly assist.


Connect-PnPOnline : The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Connect-PnPOnline https://damtek.sharepoint.com/sites/LabAs/demo -cre ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Connect-PnPOnline:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

New-PnPWeb : The term 'New-PnPWeb' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:1
+ New-PnPWeb -Title "SubSite A" -Url SubSiteA -Description "Information ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-PnPWeb:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Highlighted
@Vikram_Samal I was able to make it work via windows powershell. Thanks
Highlighted

@Dahmeey You can always  install the Pnp PowerShell assembly in your system from here:

SharePoint/PnP-PowerShell

 

Thanks for confirming that it worked for you!