Powershell scripts for creating subsite in sharepoint online

Copper Contributor
Hi, anyone please help out with cmdlets to create a subsite in sharepoint online using sharepoint online management tool. Thanks
10 Replies

@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!

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

@Dahmeey Yes I tested there and it worked

@Vikram_Samal Would try it at work tomorrow and revert. Thanks alot
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?

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!

@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!

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
@Vikram_Samal I was able to make it work via windows powershell. Thanks

@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!