The code is as follows (save to a local PS1 file, right-click Run with PowerShell):
add-PSSnapin Microsoft.SharePoint.PowerShellfunction createspweb () {$webApps= get-spwebapplication choosewebappandselectsite $webApps}function choosewebappandselectsite ($webApps) {Write-host"Web applications in the farm:"-Foregroundcolor Yellow for($i =0; $i-le $webApps. count-1; $i + +) {$tip="["+ $i. ToString () +"]"+$webApps [$i].name Write-host $tip-foregroundcolor Green} $choice= Read-host"Select The Web application, enter the number before"$siteCollections= Get-spsite-webapplication $webApps [$choice].urlif($siteCollections. Count-eq0{#Write warning tip here. Write-warning"there is no site collection under the Web application, you need to make an alternative choice."Choosewebappandselectsite $webApps}Else{Write-host"Site Collections under the Web application:"-Foregroundcolor Yellow for($i =0; $i-le $siteCollections. count-1; $i + +) {$tip="["+ $i. ToString () +"]"+$siteCollections [$i].url Write-Host $tip} $choice= Read-host"Select The Site collection, enter the number before"$tip="You want to create a new Web (subsite) under the site collection"+ $siteCollections [$i] +"? Enter ' y ' to verify"Write-host $tip-Foregroundcolor Cyan $newChoice=read-Hostif($newChoice-eq"y") {$name= Read-host"Please enter the name of the Web (subsite)"$WEBURL= $siteCollections [$choice].url +"/"+$name Write-host"the Web (subsite) is creating, please wait for a moment ..."-Foregroundcolor darkgreen New-spweb-url $webUrl-name $name-template"sts#0"Write-host"The web (subsite) has been created. The URL is above."-Foregroundcolor Green}}} Createspweb
Operating interface:
Create a script for site collection, as described in the previous blog.
Create a SharePoint Web from PowerShell