SharePoint Create a host-named Site with PowerShell

On SharePoint, we usually create site collections using the path-based route. This approach aligns very well with the SharePoint online/Office 365 site structure recommendations. With SharePoint 2013, we can create a site collection using a host-named site which allows us to assign a unique DNS name to our site collections. This enables us to deploy multiple sites with different DNS names in the same web application.

Path-named vs Host-named

Site Collection Structure

In order to deploy a host-named site, we need to create a DNS entry that points to the SharePoint host server/farm. We then need to write some code using PowerShell as this feature is not available from the Admin Central.

To create the site collection, we run this custom function from PowerShell with elevated permissions.

# Name: createHostNamedSite
# Description:  Creates a host-named site collection
# Usage
# createSite web-app-name, dns-entry site-name site-description admin-account-name site-template content-database
createHostNamedSite "ozkary.com" "https://demosite1.ozkary.com" "Demo Site 1" "Demo Site 1 Description" "admin-account" "STS#0" "WSS_Content_DB"

Parameter Information

Main SharePoint web app name where site collections are hosted
The unique DNS entry for the site
The name for the site collection
The site description
The site collection owner usually the admin
A site template like team site, document center

Click here to see more templates
The content database for this site collection

createHostNameSite Function

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#  Name: createHostNameSite
#  Description: create a host-named site collection
#  Params:
#       $webAppName 
#       $siteUrl
#       $siteName
#       $description
#       $owner
#       $template
#       $contentDatabase 
function createHostNameSite($webAppName, $siteUrl, $siteName, $description, $owner,$template, $contentDatabase)
    write-host "Creating site with parameters " $webAppName, $siteUrl, $siteName, $description, $owner,$template, $contentDatabase , $mapUrl

    $continue = Read-Host -Prompt "Do you want to continue (y/n)"
    if ($continue -eq "y")
        write-host "Provisioning Site...."       
        New-SPSite $siteUrl  -Name $siteName -Description $description -OwnerAlias $owner -Template $template  -ContentDatabase  $contentDatabase  -HostHeaderWebApplication (Get-SPWebApplication $webAppName)               

        write-host "Process cancelled"


On the code , we first include the SharePoint plug-in which enables us to use the New-SPSite cmdlet that handles all the hard work for us.  Another area to notice is that there is the HostHeaderWebApplication parameter for which we pass the SharePoint web application reference by using the Get-SPWebApplication cmdlet. This is what enables us to create the host-named site.

After the site is created, we should be able to type the DNS name, and if it is already created and propagated in your network, the request should be able to be sent to SharePoint which can load the corresponding site properly.

I hope this is helpful and enjoy your site creation automation (DevOps) with PowerShell. 

Originally published by ozkary.com


Powershell Upload Multiple Files to SharePoint

SharePoint document library enables us to manage documents. In some cases, we need to upload multiple files to that library.  A common approach to automate this process is to integrate via the SharePoint Restful APIs. In this article, we take a look at using PowerShell to create a script that can quickly enable this integration without having the need to know all the details of the API.


For our use case, we have a drop location where our files are located. We need to read all the files and upload them to a SharePoint document library.

Note: We can run this snippet using Visual Studio Code. Just save the file with an extension of ps1, and VS Code will guide you on installing the PowerShell extensions.

# include the web cmdlets
Add-PSSnapin microsoft.sharepoint.powershell

# name:  uploadFiles
# purpose: upload multiple files from a shared location
function uploadFiles($path, $siteUrl)

try {

  # gets all the files (-File) in the directory
  $files = Get-ChildItem -Path $path –File

  #iterate thru each file
  foreach($file in $files) 
      $url = $siteUrl + $file
      $filePath = $path + $file

      #upload the file to the server using default credentials from the
      #current session
      $result = Invoke-WebRequest -Uri $url -InFile $filePath -Method PUT

      #if the request status code is successful, we delete the files
      #else write an error message
      if($result.statuscode -eq 200){       
        Remove-Item $filePath -Force
        write-host "Failed to upload" $filePath $result.statuscode
catch {
  write-host "Exception was raised: $PSItem" #psitem is the error object


#uploadFiles parm0 param1
#param[0] shared-location 
#param[1] sharepoint document library url
UploadFiles "\\some-document-path" "https://ozkary.com/shared-docs/"

In this snippet, we first need to include the snap-in for SharePoint. This loads the web cmdlets that provide the implementation abstraction to the SharePoint APIs.

The uploadFiles function enables us to read a folder location and lists only the files. We then iterate thru each file and use the Invoke-WebRequest to load them to a particular SharePoint document library. If the response has Status 200, we know that the upload was a success, and we remove the file. Otherwise, we write to the standard output using write-host.


We should notice that when uploading the files, we use the -DefaultCredentials parameter.  This enables our script to pass the current session credentials to the request.  The credentials are set when the script is executed under the context of a particular service account that has access to the SharePoint document library.

 I hope this provides a quick automated way to upload files to your document libraries.

Originally published by ozkary.com