Create Artifacts in SharePoint 2013/2016 using PowerShell

This SharePoint tutorial explains various examples of PowerShell cmdlets to create artifacts in SharePoint 2013/2016.

In my previous PowerShell SharePoint examples, we saw PowerShell scripts for creating Sites and Web we learned overviewed how to create a site collection and a subsite. In this article, I would like to enhance the PowerShell knowledge to create SharePoint site artifacts like:

  • How to create Lists & Libraries using PowerShell in SharePoint 2013?
  • How to add various fields using PowerShell in SharePoint 2013?
  • How to create lookup field using PowerShell in SharePoint 2013?
  • How to add People picker field using PowerShell in SharePoint 2013?
  • How to create Choice Field using PowerShell in SharePoint 2013?
  • How to modify Views in a list using PowerShell in SharePoint 2013?

Below are a few PowerShell SharePoint 2013/2016 examples.

Create Lists & Libraries using PowerShell in SharePoint 2013/2016

As part of any site/web created in SharePoint, we need to create a list/library to store the information. Below PowerShell script will explain how to create a list & library using PowerShell in SharePoint 2013.

function CreateCustomList($siteCollectionUrl, $listName, $objType) {
$spWeb = Get-SPWeb -Identity $siteCollectionUrl
$list = $spWeb.lists[$listName]
if ($list) {
#TO permanently delete a list, Use:
$List.Delete()
Write-Host "List: $($listName) deleted successfully from: $($siteCollectionUrl)"
}
$tempType = "Custom List"
if ($objType -eq "List") {
$tempType = "Custom List"
}
else {
$tempType = "Document Library"
}

Write-Host "Creating List" $listName "with the template:"$tempType
$spTemplate = $spWeb.ListTemplates[$tempType]
$spListCollection = $spWeb.Lists
$spListCollection.Add($listName, $listName, $spTemplate)
$spListCollection.Update()
$spWeb.close()
}

$siteCollectionUrl = "http://sharepoint13:12345/"
$listName = "Technology Details"
$docLibName = "Technology Repository"

CreateCustomList $siteCollectionUrl $listName "List"

CreateCustomList $siteCollectionUrl $docLibName "Library"

In the above script block, I am creating one custom list and one document library. In the MSDN blog you can see all other list/library template types.

Add fields to SharePoint List using PowerShell

In a SharePoint list, we can create any number of fields in the below code block I am creating numbers, text and multiline text fields. You can verify all other field types in msdn blog:

# Creating fields in List
$siteCollectionUrl = "http://sharepoint13:12345/"
$spWeb = Get-SPWeb -Identity $siteCollectionUrl
$listName = "Technology Details"
$spList = $spWeb.lists[$listName]
if ($spList) {

#adding the field type(Number) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
$spList.Fields.Add("Salary", $spFieldType, $false)

#adding the field type(Text) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$spList.Fields.Add("First Name", $spFieldType, $true)

#adding the field type(Multilines text) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Note
$spList.Fields.Add("Address", $spFieldType, $true)
$spList.Update()
}
else {
Write-Host "List" $listName "not existed"
}
$spWeb.close()

Create a lookup field using PowerShell in SharePoint 2013 List

Creating a lookup column in a list is a little tricky than creating any other column, here is the PowerShell script to create a lookup column in SharePoint 2013/2016.

$SiteUrl = "http://sharepoint13:12345/"
$web = Get-SPWeb -identity $SiteUrl
$ParentLstLookupclmnName = 'Title'
$chldListLookupclmnName = "Technology Name"
$ParentList = $web.Lists['Technology Details']
$ChildList = $web.Lists['Opportunities']

#Check if Field exists already
if (!$childList.Fields.ContainsField($chldListLookupclmnName)) {

#sharepoint powershell – create lookup field
$chldListLookupclmn = $ChildList.Fields.AddLookup($chldListLookupclmnName, $ParentList.id, $False)
$chldListLookupclmn = $ChildList.Fields[$chldListLookupclmnName]

#Setup lookup Field property
$chldListLookupclmn.LookupField = $ParentList.Fields[$ParentLstLookupclmnName]
$chldListLookupclmn.Required = $true
$chldListLookupclmn.AllowMultipleValues = $true
$chldListLookupclmn.update()
write-host "Lookup field added successfully!" -f green
}
else {
write-host "Field Exists already!" -f red
}
$web.close()

Add People picker field using PowerShell in SharePoint 2013/2016

Below is the PowerShell script to add people picker field using PowerShell in SharePoint 2013/2016.

$webURL = "http://sharepoint13:12345/"
$web = Get-SPWeb $webURL
$mylistName = "Opportunities"
$mylist = $web.Lists[$mylistName]
$spFieldType = [Microsoft.SharePoint.SPFieldType]::User
Write-Host $spFieldType
$mylist.Fields.Add("Manager Name", $spFieldType, $true)
$mylist.Update()
$web.close()

If you want to update the field type of people picker using Powershell.

$site = Get-SPSite "http://sharepoint13:12345/"
$web=$site.OpenWeb()
$list = $web.Lists["Opportunities"]
$spFieldUser = [Microsoft.SharePoint.SPFieldUser]($list.Fields["Manager Name"])
$spFieldUser.LookupField = "EMail"
$spFieldUser.Update()
$web.close()

Below are the lists of various other lookupfield type values:

  • Email –> EMail
  • Title –> Title
  • Account –> Name
  • Name (with presence) –> ImnName
  • ID –> ID
  • Modified –> Modified
  • Job Title –> JobTitle
  • Name(With Picture And Details) –> NameWithPictureAndDetails

Create Choice Field using PowerShell in SharePoint 2013

Now, we will see how to create a choice column using PowerShell in SharePoint 2013/2016.

$webURL = "http://sharepoint13:12345/"
$web = Get-SPWeb $webURL
$mylistName = "Opportunities"
$spList = $web.Lists[$mylistName]
$choices = New-Object System.Collections.Specialized.StringCollection
$choices.Add("First Choice")
$choices.Add("Second Choice")
$choices.Add("Third Choice")
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Choice
$spList.Fields.Add("ChoiceField",$spFieldType,$false,$false,$choices)
$spList.Update()
$web.close()

Modify Views in a list using PowerShell in SharePoint 2013/2016

Now that we have completed creating lists and fields, let us add the list to quick launch and add new fields to views.

Adding to the quick launch bar & modifying the view:

$spWeb = Get-SPWeb -Identity http://sharepoint13:12345/
$spList = $spWeb.GetList("/Lists/Opportunities")
$spList.OnQuickLaunch = $true
$spView = $spWeb.GetViewFromUrl("/Lists/Opportunities/AllItems.aspx")
$spField = $spList.Fields["Technology Name"]
$spView.ViewFields.Add($spField)
$spView.Update()
$spList.Update()
$spWeb.close()

If you want to add a field to a specific view:

$spWeb = Get-SPWeb -Identity http://sharepoint13:12345/
$spList = $spWeb.Lists["Opportunities"]
$spView = $spList.Views | ? { $_.Title -eq 'Technologies' }
$spField = $spList.Fields["Technology Name"]
$spView.ViewFields.Add($spField)
$spView.Update()
$spWeb.close()

Show/Hide a SharePoint 2013 List using PowerShell

Recently my team got a requirement to hide a SharePoint 2013 list. Here we will discuss how to show or hide a SharePoint 2013 list using PowerShell. The user should not see the list in Site content as well as in Quick Launch.

Here I have a list name as “MyTestList99” which I want to hide from the site content.

PowerShell script to hide a SharePoint 2013 List

Below is the PowerShell script to hide a SharePoint List, which you can write in Windows PowerShell ISE or you can use Visual studio code to write, debug and test PowerShell scripts.

Open SharePoint Management Shell

Add-PSSnapin Microsoft.SharePoint.Powershell
$web = Get-SPWeb "http://win-pfcp2dgt8di/sites/EnjoySharePoint/";
$list = $web.Lists["MyTestList99"];
$list.Hidden = 1; //hiding the list in Site Contents
$list.OnQuickLaunch = $false; // hiding the list in Quick Launch
$list.Update();

PowerShell script to Show a SharePoint 2013 List

Below is the PowerShell script to Show a SharePoint List:

Add-PSSnapin Microsoft.SharePoint.Powershell
$web = Get-SPWeb "http://win-pfcp2dgt8di/sites/EnjoySharePoint/";
$list = $web.Lists["MyTestList99"];
$list.Hidden = 0; //showing the list in Site Contents
$list.OnQuickLaunch = $true; // showing the list in Quick Launch
$list.Update();

Hope this will be useful to you to show or hide a SharePoint 2013 list or document library.

You may like following PowerShell SharePoint tutorials:

This SharePoint PowerShell tutorial, we discussed how to create lists and libraries using PowerShell in SharePoint 2013/2016.

We also saw how to add various types of fields like lookup, people picker, a Choice column in SharePoint using PowerShell. We also saw, how to modify SharePoint list views using PowerShell.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>