Before reading this article, please go through the following article:

Table of Contents
  1. Introduction

    • Document Objective
    • Audience

  2. Creating Site Collection and then sub site within same site collection.

    • Procedure

  3. Creating Sub Site under already created Site collection.

    • Procedure

  4. PowerShell Script

Introduction

  1. Document Objective

    The purpose of this document is to provide instructions on using this PowerShell script to create site collections and sites in SharePoint using different web templates.

  2. Audience

    This document is targeted to developers and architects responsible for creating site collections and sites using multiple combinations with different web templates. Please note that root site collection must be present with web application URL. The code can be modified and extended as per requirements.

Creating Site Collection and then sub site within same site collection

Procedure

  1. Go to Start > Open SharePoint 2013 Management Shell.

  2. Run as administrator.



  3. Run the script.



  4. Enter Web application url. Press Enter.



  5. It will validate the web application url. If root site collection is present at web application url, it will ask if user wants to create site collection or sub site.



  6. Enter 1. It will ask the site collection details. Enter Site Title, Site description and Site Url (must be a valid URL) URL is pre-populated in ‘e.g’ part. User can use this URL.



  7. After entering all details, it will ask for web template category choice.

    Enter 1. Then, it will ask for web template for this particular web template category.
    Enter 1 for Team Site.



  8. Enter Primary administrator and Secondary Administrator (optional) details.Press Enter.



  9. Site Provisioning process will take place.



  10. After Site Collection got provisioned, it will ask if user wants sub site to be created under newly created site collection.



  11. Enter “y”. It will ask for Sub Site details. Enter these details.



  12. Site Provisioning process will take place.



  13. After successfully provisioning the site, it will ask if another sub site to be created under same site collection.



  14. Enter “y” if user wants another sub site to be created under same site collection. Otherwise enter “n”.



  15. Then it will ask if another site collection under same web application to be created.

    Enter “y” if user wants another site collection to be created under same web application. Otherwise enter “n”.



  16. Then it will ask if another Site collection or sub site to be created. Enter “y”.



  17. User can follow steps 5 to 13 to create multiple site collections and sub sites with different web templates.

Creating Sub Site under already created Site collection

Procedure

  1. Go to Start > Open SharePoint 2013 Management Shell.

  2. Run as administrator.

  3. Run the script.

  4. Enter Web application url. Press Enter.

  5. It will validate the web application url. If root site collection is present at web application url, it will ask if user wants to create site collection or sub site.

  6. Enter 2.



  7. Enter the site collection url under which the sub site is to be created.



    It will validate the entered site collection URL.

  8. Enter the sub site details. Select ‘Records Center’ web template from ‘Enterprise Site template’ category.



  9. As Records center needs Publishing feature to be activated on parent site collection. So first Publishing feature is activated at parent site collection and then sub site will be provisioned.



  10. After provisioning sub site successfully, it will ask if another sub site to be created within same site collection.

  11. Enter “y” if user wants another sub site to be created under same site collection. Otherwise enter “n”.

  12. Then it will ask if another Site collection or sub site to be created. Enter “y” if user wants another site collection or sub site to be created. Otherwise, enter “n”.

PowerShell Script

  1. if ((Get - PSSnapin | Where
  2. {
  3. $_.Name - eq "Microsoft.SharePoint.PowerShell"
  4. }) - eq $null)
  5. {
  6. Add - PSSnapinMicrosoft.SharePoint.PowerShell;
  7. }
  8. ##Load SharePoint DLL
  9. [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
  10. functionGetUserInput([string] $sourceWebURL)
  11. {
  12. try
  13. {
  14. write - host "Validating if the entered site exists...$sourceWebURL"
  15. $site = Get - SPSite $sourceWebURL - ErrorActionSilentlyContinue
  16. if ($site - ne $null)
  17. {
  18. write - host "Successfully Validated the following Site Collection: $sourceWebURL" - ForegroundColor Green
  19. return $true;
  20. }
  21. else
  22. {
  23. return $false;
  24. }
  25. }
  26. catch [System.Exception]
  27. {
  28. DisplayErrorMsg($_.Exception.ToString())
  29. }
  30. }
  31. functionCreateSiteCollection([string] $sourceWebURL)
  32. {
  33. try
  34. {
  35. $spSourceWeb = Get - SPWeb $sourceWebURL
  36. $spSite = Get - SPSite($sourceWebURL)
  37. $spWeb = $spSite.OpenWeb()
  38. $createSiteColl = "Y"
  39. while ($createSiteColl - eq "Y" - or $createSiteColl - eq "y")
  40. {
  41. $SiteDescription = $null
  42. write - host "Please enter the Site Title : " - ForegroundColor Yellow
  43. $SiteTitle = read - host
  44. write - host "Please enter the Site Description : " - ForegroundColor Yellow
  45. $SiteDescription = read - host
  46. $spaceremovedsitetitle = $SiteTitle - replace " ", ""
  47. write - host "Please enter the Site Collection URL : (e.g. $sourceWebURL/sites/$spaceremovedsitetitle)" - ForegroundColor Yellow
  48. $sourceSiteCollURL = $sourceWebURL += "/sites/"
  49. $SiteUrltemp = read - host $sourceSiteCollURL
  50. $SiteUrltemp = $SiteUrltemp - replace " ", ""
  51. $SiteUrl = $sourceSiteCollURL += $SiteUrltemp
  52. Write - Host Site Collection URL is: $SiteUrl - ForegroundColor Green
  53. $SpaceRemovedURL = $SiteUrl.trimend(" ")
  54. $SiteUrl = $SpaceRemovedURL
  55. $lastChar = $SiteUrl.Substring($SiteUrl.Length - 1, 1);
  56. if ($lastChar - eq "/")
  57. {
  58. $SiteUrl = $SiteUrl.Substring(0, $SiteUrl.Length - 1);
  59. }
  60. else
  61. {
  62. $SiteUrl = $SiteUrl;
  63. }
  64. $invalid = $false
  65. write - host "Please select the category for Site Template :" - ForegroundColor Cyan
  66. Write - Host "1 :Collboration Site Templates" - ForegroundColor Yellow
  67. Write - Host "2 : Enterprise Site Templates" - ForegroundColor Yellow
  68. Write - Host "3 : Publishing Site Templates" - ForegroundColor Yellow
  69. $category = Read - Host
  70. if ($category - eq 1)
  71. {
  72. write - host "Please select the Site Template :" - ForegroundColor Cyan
  73. Write - Host "1: Team Site" - ForegroundColor Yellow
  74. Write - Host "2: Blog" - ForegroundColor Yellow
  75. Write - Host "3: Developer Site" - ForegroundColor Yellow
  76. Write - Host "4: Project Site" - ForegroundColor Yellow
  77. Write - Host "5: Community Site" - ForegroundColor Yellow
  78. }
  79. elseif($category - eq 2)
  80. {
  81. write - host "Please select the Site Template :" - ForegroundColor Cyan
  82. Write - Host "1: Document Center" - ForegroundColor Yellow
  83. Write - Host "2: eDiscovery Center" - ForegroundColor Yellow
  84. Write - Host "3: Records Center" - ForegroundColor Yellow
  85. Write - Host "4: Business Intelligence Center" - ForegroundColor Yellow
  86. Write - Host "5: Enterprise Search Center" - ForegroundColor Yellow
  87. Write - Host "6: My Site Host" - ForegroundColor Yellow
  88. Write - Host "7: Community Portal" - ForegroundColor Yellow
  89. Write - Host "8: Basic Search Center" - ForegroundColor Yellow
  90. Write - Host "9: Visio Process Repository" - ForegroundColor Yellow
  91. }
  92. elseif($category - eq 3)
  93. {
  94. write - host "Please select the Site Template :" - ForegroundColor Cyan
  95. Write - Host "1: Publishing Portal" - ForegroundColor Yellow
  96. Write - Host "2: Enterprise Wiki " - ForegroundColor Yellow
  97. Write - Host "3: Product Catalog" - ForegroundColor Yellow
  98. }
  99. else
  100. {
  101. write - host "Please select valid Site template Category. Try again." - ForegroundColor Red
  102. $invalid = $true
  103. }
  104. if ($invalid - ne $true)
  105. {
  106. $templateType = read - host
  107. $primaryadmin = $null
  108. $secondaryAdmin = $null
  109. Write - Host "Please enter Primary Administrator(domainname\loginnamee.gcorp\administrator)" - ForegroundColor Yellow
  110. $userValue = read - host[Microsoft.SharePoint.SPUser] $primaryadmin = $spWeb.EnsureUser($userValue)
  111. Write - Host "Enter Secondary Administrator <Y/N>?" - ForegroundColor Yellow
  112. $IsSecondaryAdmin = read - host
  113. if ($IsSecondaryAdmin - eq "Y" - or $IsSecondaryAdmin - eq "y")
  114. {
  115. Write - Host "Please enter Secondary Administrator(domainname\loginnamee.gcorp\administrator)" - ForegroundColor Yellow
  116. $userSecondaryValue = read - host[Microsoft.SharePoint.SPUser] $secondaryAdmin = $spWeb.EnsureUser($userSecondaryValue)
  117. }
  118. if ($category - eq 1 - and $templateType - eq 1)
  119. {
  120. if ($secondaryAdmin - ne $null)
  121. {
  122. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  123. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "STS#0" - Language 1033
  124. }
  125. else
  126. {
  127. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  128. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "STS#0" - Language 1033
  129. }
  130. }
  131. elseif($category - eq 1 - and $templateType - eq 2)
  132. {
  133. if ($secondaryAdmin - ne $null)
  134. {
  135. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  136. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "BLOG#0" - Language 1033
  137. }
  138. else
  139. {
  140. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  141. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "BLOG#0" - Language 1033
  142. }
  143. }
  144. elseif($category - eq 1 - and $templateType - eq 3)
  145. {
  146. if ($secondaryAdmin - ne $null)
  147. {
  148. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  149. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "DEV#0" - Language 1033
  150. }
  151. else
  152. {
  153. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  154. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "DEV#0" - Language 1033
  155. }
  156. }
  157. elseif($category - eq 1 - and $templateType - eq 4)
  158. {
  159. if ($secondaryAdmin - ne $null)
  160. {
  161. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  162. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "PROJECTSITE#0" - Language 1033
  163. }
  164. else
  165. {
  166. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  167. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "PROJECTSITE#0" - Language 1033
  168. }
  169. }
  170. elseif($category - eq 1 - and $templateType - eq 5)
  171. {
  172. if ($secondaryAdmin - ne $null)
  173. {
  174. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  175. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "COMMUNITY#0" - Language 1033
  176. }
  177. else
  178. {
  179. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  180. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "COMMUNITY#0" - Language 1033
  181. }
  182. }
  183. elseif($category - eq 2 - and $templateType - eq 1)
  184. {
  185. if ($secondaryAdmin - ne $null)
  186. {
  187. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  188. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "BDR#0" - Language 1033
  189. }
  190. else
  191. {
  192. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  193. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "BDR#0" - Language 1033
  194. }
  195. }
  196. elseif($category - eq 2 - and $templateType - eq 2)
  197. {
  198. if ($secondaryAdmin - ne $null)
  199. {
  200. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  201. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "EDISC#0" - Language 1033
  202. }
  203. else
  204. {
  205. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  206. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "EDISC#0" - Language 1033
  207. }
  208. }
  209. elseif($category - eq 2 - and $templateType - eq 3)
  210. {
  211. if ($secondaryAdmin - ne $null)
  212. {
  213. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  214. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "OFFILE#1" - Language 1033
  215. }
  216. else
  217. {
  218. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  219. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "OFFILE#1" - Language 1033
  220. }
  221. }
  222. elseif($category - eq 2 - and $templateType - eq 4)
  223. {
  224. if ($secondaryAdmin - ne $null)
  225. {
  226. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  227. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "BICenterSite#0" - Language 1033
  228. }
  229. else
  230. {
  231. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  232. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "BICenterSite#0" - Language 1033
  233. }
  234. }
  235. elseif($category - eq 2 - and $templateType - eq 5)
  236. {
  237. if ($secondaryAdmin - ne $null)
  238. {
  239. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  240. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "SRCHCEN#0" - Language 1033
  241. }
  242. else
  243. {
  244. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  245. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "SRCHCEN#0" - Language 1033
  246. }
  247. }
  248. elseif($category - eq 2 - and $templateType - eq 6)
  249. {
  250. if ($secondaryAdmin - ne $null)
  251. {
  252. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  253. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "SPSMSITEHOST#0" - Language 1033
  254. }
  255. else
  256. {
  257. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  258. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "SPSMSITEHOST#0" - Language 1033
  259. }
  260. }
  261. elseif($category - eq 2 - and $templateType - eq 7)
  262. {
  263. if ($secondaryAdmin - ne $null)
  264. {
  265. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  266. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "COMMUNITYPORTAL#0" - Language 1033
  267. }
  268. else
  269. {
  270. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  271. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "COMMUNITYPORTAL#0" - Language 1033
  272. }
  273. }
  274. elseif($category - eq 2 - and $templateType - eq 8)
  275. {
  276. if ($secondaryAdmin - ne $null)
  277. {
  278. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  279. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "SRCHCENTERLITE#0" - Language 1033
  280. }
  281. else
  282. {
  283. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  284. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "SRCHCENTERLITE#0" - Language 1033
  285. }
  286. }
  287. elseif($category - eq 2 - and $templateType - eq 9)
  288. {
  289. if ($secondaryAdmin - ne $null)
  290. {
  291. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  292. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "visprus#0" - Language 1033
  293. }
  294. else
  295. {
  296. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  297. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "visprus#0" - Language 1033
  298. }
  299. }
  300. elseif($category - eq 3 - and $templateType - eq 1)
  301. {
  302. if ($secondaryAdmin - ne $null)
  303. {
  304. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  305. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "BLANKINTERNETCONTAINER#0" - Language 1033
  306. }
  307. else
  308. {
  309. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  310. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "BLANKINTERNETCONTAINER#0" - Language 1033
  311. }
  312. }
  313. elseif($category - eq 3 - and $templateType - eq 2)
  314. {
  315. if ($secondaryAdmin - ne $null)
  316. {
  317. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  318. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "ENTERWIKI#0" - Language 1033
  319. }
  320. else
  321. {
  322. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  323. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "ENTERWIKI#0" - Language 1033
  324. }
  325. }
  326. elseif($category - eq 3 - and $templateType - eq 3)
  327. {
  328. if ($secondaryAdmin - ne $null)
  329. {
  330. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  331. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - SecondaryOwnerAlias $secondaryAdmin - Name $SiteTitle– Description $SiteDescription - Template "PRODUCTCATALOG#0" - Language 1033
  332. }
  333. else
  334. {
  335. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  336. $siteColl = New - SPSite– Url $SiteUrl - OwnerAlias $primaryadmin - Name $SiteTitle– Description $SiteDescription - Template "PRODUCTCATALOG#0" - Language 1033
  337. }
  338. }
  339. else
  340. {
  341. write - host "Please select valid Site template" - ForegroundColor Red
  342. }
  343. }
  344. if ($siteColl - ne $null)
  345. {
  346. $siteCollUrl = $siteColl.Url
  347. Write - Host "Site Collection created successfully." - ForegroundColor Green
  348. Write - Host "Site Collection URL is $siteCollUrl" - ForegroundColor Green
  349. Write - Host "Create Sub Site under newly created site collection<Y/N>?" - ForegroundColor Yellow
  350. $createSubSite = read - host
  351. if ($createSubSite - eq "Y" - or $createSubSite - eq "y")
  352. {
  353. CreateSubSite $siteCollUrl
  354. }
  355. }
  356. else
  357. {
  358. Write - Host "Error creating Site Collection. Please try again." - ForegroundColor Red
  359. }
  360. write - host "Create Another Site Collection under same web aplication<Y/N>?" - foregroundcolor Yellow
  361. $createSiteColl = Read - Host
  362. }
  363. }
  364. catch [System.Exception]
  365. {
  366. Write - Host $_.Exception.ToString()
  367. }
  368. }
  369. functionCreateSubSite([string] $SiteCollUrl)
  370. {
  371. try
  372. {
  373. $createanotherSubSite = "Y"
  374. while ($createanotherSubSite - eq "Y" - or $createanotherSubSite - eq "y")
  375. {
  376. $featureIdentity = "PublishingSite"
  377. $SubSiteDescription = $null
  378. write - host "Please enter the Sub Site Title : " - ForegroundColor Yellow
  379. $SubSiteTitle = read - host
  380. write - host "Please enter the Sub Site Description : " - ForegroundColor Yellow
  381. $SubSiteDescription = read - host
  382. $SpaceRemovedTitle = $SubSiteTitle - replace " ", ""
  383. $SiteCollUrlModified = $SiteCollUrl + "/"
  384. $SubSiteUrl = $SiteCollUrlModified += $SpaceRemovedTitle
  385. Write - Host $SubSiteUrl
  386. write - host "Please select the category for Site Template :" - ForegroundColor Cyan
  387. Write - Host "1 :Collboration Site Templates" - ForegroundColor Yellow
  388. Write - Host "2 : Enterprise Site Templates" - ForegroundColor Yellow
  389. $category = Read - Host
  390. $invalid = $false
  391. if ($category - eq 1)
  392. {
  393. write - host "Please select the Site Template :" - ForegroundColor Cyan
  394. Write - Host "1: Team Site" - ForegroundColor Yellow
  395. Write - Host "2: Blog" - ForegroundColor Yellow
  396. Write - Host "3: Project Site" - ForegroundColor Yellow
  397. Write - Host "4: Community Site" - ForegroundColor Yellow
  398. }
  399. elseif($category - eq 2)
  400. {
  401. write - host "Please select the Site Template :" - ForegroundColor Cyan
  402. Write - Host "1: Document Center" - ForegroundColor Yellow
  403. Write - Host "2: Records Center" - ForegroundColor Yellow
  404. Write - Host "3: Business Intelligence Center" - ForegroundColor Yellow
  405. Write - Host "4: Enterprise Search Center" - ForegroundColor Yellow
  406. Write - Host "5: Basic Search Center" - ForegroundColor Yellow
  407. Write - Host "6: Visio Process Repository" - ForegroundColor Yellow
  408. }
  409. else
  410. {
  411. write - host "Please select valid Site template Category. Try again." - ForegroundColor Red
  412. $invalid = $true
  413. }
  414. if ($invalid - ne $true)
  415. {
  416. $templateType = read - host
  417. if ($category - eq 1 - and $templateType - eq 1)
  418. {
  419. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  420. $subsite = New - SPWeb $SubSiteUrl - Template "STS#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  421. }
  422. elseif($category - eq 1 - and $templateType - eq 2)
  423. {
  424. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  425. $subsite = New - SPWeb $SubSiteUrl - Template "BLOG#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  426. }
  427. elseif($category - eq 1 - and $templateType - eq 3)
  428. {
  429. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  430. $subsite = New - SPWeb $SubSiteUrl - Template "PROJECTSITE#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  431. }
  432. elseif($category - eq 1 - and $templateType - eq 4)
  433. {
  434. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  435. $subsite = New - SPWeb $SubSiteUrl - Template "COMMUNITY#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  436. }
  437. elseif($category - eq 2 - and $templateType - eq 1)
  438. {
  439. Write - Host "Enabling Publishing feature on Top Site Collection..Please wait.." - ForegroundColor Yellow
  440. Enable - SPFeature - identity $featureIdentity - URL $SiteCollUrl - Force
  441. Write - Host Successfully enabled Publishing feature on Top Site Collection: $SiteCollUrl - ForegroundColor Green
  442. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  443. $subsite = New - SPWeb $SubSiteUrl - Template "BDR#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  444. }
  445. elseif($category - eq 2 - and $templateType - eq 2)
  446. {
  447. Write - Host "Enabling Publishing feature on Top Site Collection..Please wait.." - ForegroundColor Yellow
  448. Enable - SPFeature - identity $featureIdentity - URL $SiteCollUrl - Force
  449. Write - Host Successfully enabled Publishing feature on Top Site Collection: $SiteCollUrl - ForegroundColor Green
  450. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  451. $subsite = New - SPWeb $SubSiteUrl - Template "OFFILE#1" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  452. }
  453. elseif($category - eq 2 - and $templateType - eq 3)
  454. {
  455. Write - Host "Enabling Publishing feature on Top Site Collection..Please wait.." - ForegroundColor Yellow
  456. Enable - SPFeature - identity $featureIdentity - URL $SiteCollUrl - Force
  457. Write - Host Successfully enabled Publishing feature on Top Site Collection: $SiteCollUrl - ForegroundColor Green
  458. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  459. $subsite = New - SPWeb $SubSiteUrl - Template "BICenterSite#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  460. }
  461. elseif($category - eq 2 - and $templateType - eq 4)
  462. {
  463. Write - Host "Enabling Publishing feature on Top Site Collection..Please wait.." - ForegroundColor Yellow
  464. Enable - SPFeature - identity $featureIdentity - URL $SiteCollUrl - Force
  465. Write - Host Successfully enabled Publishing feature on Top Site Collection: $SiteCollUrl - ForegroundColor Green
  466. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  467. $subsite = New - SPWeb $SubSiteUrl - Template "SRCHCEN#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  468. }
  469. elseif($category - eq 2 - and $templateType - eq 5)
  470. {
  471. Write - Host "Enabling Publishing feature on Top Site Collection..Please wait.." - ForegroundColor Yellow
  472. Enable - SPFeature - identity $featureIdentity - URL $SiteCollUrl - Force
  473. Write - Host Successfully enabled Publishing feature on Top Site Collection: $SiteCollUrl - ForegroundColor Green
  474. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  475. $subsite = New - SPWeb $SubSiteUrl - Template "SRCHCENTERLITE#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  476. }
  477. elseif($category - eq 2 - and $templateType - eq 6)
  478. {
  479. Write - Host "Enabling Publishing feature on Top Site Collection..Please wait.." - ForegroundColor Yellow
  480. Enable - SPFeature - identity $featureIdentity - URL $SiteCollUrl - Force
  481. Write - Host Successfully enabled Publishing feature on Top Site Collection: $SiteCollUrl - ForegroundColor Green
  482. Write - Host "Please wait..Site Provisioning in Progress.." - ForegroundColor Cyan
  483. $subsite = New - SPWeb $SubSiteUrl - Template "visprus#0" - Name $SubSiteTitle– Description $SubSiteDescription - UseParentTopNav - Language 1033
  484. }
  485. else
  486. {
  487. Write - Host "Please select valid Site template" - ForegroundColor Red
  488. }
  489. if ($subsite - ne $null)
  490. {
  491. Write - Host "Sub Site Created Successfully" - ForegroundColor Green
  492. Write - Host "SubSite URL is $SubSiteUrl" - ForegroundColor Green
  493. }
  494. else
  495. {
  496. Write - Host "Error creating SubSite. Please try again." - ForegroundColor Red
  497. }
  498. }
  499. Write - Host "Create another Sub Site under same site collection<Y/N>?" - ForegroundColor Yellow
  500. $createanotherSubSite = read - host
  501. }
  502. }
  503. catch [System.Exception]
  504. {
  505. Write - Host $_.Exception.ToString()
  506. }
  507. }
  508. functionSitecollectionorsubsite
  509. {
  510. Write - Host "Please select the choice" - foregroundcolor Cyan
  511. Write - Host "1. Site Collection" - foregroundcolor Yellow
  512. Write - Host "2. Sub Site" - foregroundcolor Yellow
  513. $sitecollorsite = Read - Host
  514. if ($sitecollorsite - eq 1 - or $sitecollorsite - eq 2)
  515. {
  516. return $sitecollorsite
  517. }
  518. else
  519. {
  520. Write - Host "Please select the valid option and try running script again" - ForegroundColor Red
  521. return 3
  522. }
  523. }
  524. write - host "Please enter Web Application url" - foregroundcolor Yellow
  525. $sourceWebURL = Read - Host
  526. $WebApp = get - spwebapplication $sourceWebURL - ErrorActionsilentlycontinue
  527. if ($WebApp - eq $null)
  528. {
  529. Write - host "Web Application at above url does not Exists. Please Enter valid URL. Try again." - foregroundcolor Red
  530. }
  531. else
  532. {
  533. Write - host "Successfully validated the web application.." - foregroundcolor Green
  534. $isrootsiteexist = GetUserInput $sourceWebURL
  535. if ($isrootsiteexist - eq $true)
  536. {
  537. $sitecollorsite = Sitecollectionorsubsite
  538. while ($sitecollorsite - eq 1 - or $sitecollorsite - eq 2)
  539. {
  540. if ($sitecollorsite - eq 1)
  541. {
  542. while ($sitecollorsite - eq 1)
  543. {
  544. CreateSiteCollection $sourceWebURL
  545. Write - Host "Create Another Site Collection or Sub Site (Y/N)" - ForegroundColor Yellow
  546. $anothersitecollorsite = Read - Host
  547. if ($anothersitecollorsite - eq "Y" - or $anothersitecollorsite - eq "y")
  548. {
  549. $sitecollorsite = Sitecollectionorsubsite
  550. }
  551. else
  552. {
  553. $sitecollorsite = 3
  554. Write - Host "Thank you!!.." - ForegroundColor Green
  555. }
  556. }
  557. }
  558. elseif($sitecollorsite - eq 2)
  559. {
  560. while ($sitecollorsite - eq 2)
  561. {
  562. Write - Host "Please enter the site collection url under which sub site to be created" - ForegroundColor Yellow
  563. $sitecollurl = Read - Host
  564. $isSiteExist = GetUserInput $sitecollurl
  565. if ($isSiteExist - eq $true)
  566. {
  567. CreateSubSite $sitecollurl
  568. }
  569. else
  570. {
  571. write - host "The Site Collection at : $sitecollurl , doesn't exist!!..Please Try again." - ForegroundColor Red
  572. }
  573. Write - Host "Create Another Site Collection or Sub Site (Y/N)" - ForegroundColor Yellow
  574. $anothersitecollorsite = Read - Host
  575. if ($anothersitecollorsite - eq "Y" - or $anothersitecollorsite - eq "y")
  576. {
  577. $sitecollorsite = Sitecollectionorsubsite
  578. }
  579. else
  580. {
  581. $sitecollorsite = 3
  582. Write - Host "Thank you!!.." - ForegroundColor Green
  583. }
  584. }
  585. }
  586. }
  587. }
  588. else
  589. {
  590. write - host "The Root Site Collection at : $sourceWebURL , doesn't exist!!..Please Try again." - ForegroundColor Red
  591. }
  592. }