A project at work recently required me to create in the neighborhood of 400 new SharePoint sites. I figured this would be an opportunity to take my simple Powershell script to create a SharePoint team site and expand on it. I have seen scripts that can import CSV files into lists that a former coworker and much better coder than me showed me at our last place of work, and I set out to see if the same could be applied to the site creation process. Luckily, the values I was given for the sites were based off predetermined values and delivered to me in an Excel document that I was able to copy to a column to remove spaces for the URL, add a header row, and save as a .csv file.
One thought I had going into this process is that it's going to save me a lot of time and space in my admin view if I can separate this batch of sites out. I figured the best way to do this was to assign them all the same hub site so I could a) search all the sites at the same time, and b) clean them out of my views quickly. Those were the two big modifications I made to the script for creating a basic team site. I also hard coded in some values that I prompted for in the previous code to further automate things.
The code should be pretty well documented. I ran into a couple of hiccups along the way that hopefully I can help you with here. First, make sure the admin user is allowed to associate sites with your hub site. I have two accounts, my normal and admin accounts. Typically when I set up a site with my other script, I use my traditional account as the site admin for ease. In doing that with my hub site, it didn't automatically allow my admin account to assign the sites to the hub. My first tests were all isolated. Which brings me to my second point. Don't set up and let it rip without a test. If I were to add 400 sites the first time the script ran, I'd have a mess. Make sure it's working before you walk away. Finally, hub sites take an extra minute or two to populate in the admin pannel. As your watching, give it a little extra time before you pull the plug on the code.
Other than that, you should hopefully be good. Here's the code and comment with any questions or concerns.
##############################################################################
#
# SharePoint site Creation from CSV
# by Matt Peterson
# 3/25/2019
#
#############################################################################
# Connecting to your domain - will use Credential Manager to pull in Admin Credentials
Connect-PnPOnline -Url https://[YOURDOMAIN].sharepoint.com -Credentials:SharePointAdmin
# Location of the CSV
$csv = Import-Csv -path "[YOUR:\FILE\LOCATION]"
# Set global variables - I hard code these - use the same user that's in your admin credentials
$AdminUser = 'USERNAME'
$RequestingUser = 'HUBSITE'
# This command loops the site creation for each row in the CSV
foreach ($row in $csv)
{
#The CSV has the headers of SiteName and SiteAddress, so we set the site variables on those columns
$SiteName = $row."SiteName"
$SiteURL = $row."SiteAddress"
# Creating the Team Site
New-PnPTenantSite `
-Title "$SiteName" `
-Url "https://[YOURDOMAIN].sharepoint.com/sites/$SiteURL" `
-Description "[SITEDESCRIPTION]" `
-Owner "$AdminUser" `
-Lcid 1033 `
-Template "STS#3" ` # standard template
-TimeZone 11 ` # US Central Timezone
-Wait
# Update Tracker List - this is optional but I recommend having a place you keep track of created sites
# Connect to site where list lives
Connect-PnPOnline -Url "https://[YOURDOMAIN].sharepoint.com/sites/[LISTSITE]/" -Credentials:SharePointAdmin
# Add list item - you will probably need to rename the headers to match your list
$list = Get-PnPList -Identity "[NAME of LIST]"
$ListValues = @{"Title" = "$SiteName"; "Admin_x0020_Email" = "$AdminUser"; "Site_x0020_URL" = "https://nmdpbtm.sharepoint.com/sites/$SiteURL"; "Built_x0020_For" = "$RequestingUser"}
Add-PnPListItem -List $list -ContentType "Item" -Values $ListValues
# Full site and Hubsite URL Variables
$fullsiteURL = "https://[YOURDOMAIN].sharepoint.com/sites/$SiteURL"
$hubURL = "https://nmdpbtm.sharepoint.com/sites/[HUBSITE]"
#Check variables - optional step, helpful in troubleshooting
"$fullsiteURL"
"$hubURL"
# Associate our new site to the created hubsite
Add-PnPHubSiteAssociation -Site $fullsiteURL -HubSite $hubURL
}
Comments