Changing required status for site columns in content type (PowerShell)  

Introduction

 Changing required status for site columns in content type. 

Scenarios

The required status for site columns have two options, required and optional .This script is to change required status for site columns in content type.

Script 

This script contains one advanced function Set-OSCContentTypeStatus, You can use this script in the following ways: 

Method 1:

  1. Download the script and open the script file with Notepad or any other script editors.
  2. Scroll down to the end of the script file, and then add the example command which you want to run.
  3. Save the file then run the script in PowerShell.  

Method 2: 

1.    Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
2.    Run Import-Module cmdlet to import this module file.
        Import-Module filepath\scriptname.psm1

Examples

Example 1: Get help about Set-OSCContentTypeStatus       
Command
:   Get-help   Set-OSCContentTypeStatus -Full
Screenshot

Example 2: Change required status for site columns in content type into required.
Command:   Set-OSCContentTypeStatus -SiteUrl
"http://sp-server/sites/test1" -ContentName "Audio" -ColumnName "Preview" -Required
Screenshot:

Example 3: Change required status for site columns in content type into optional.
Command:   Set-OSCContentTypeStatus -SiteUrl "http://sp-server/sites/test1" -ContentName "Audio" -ColumnName "Preview" -Optional
Screenshot:


Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
$site=Get-SPSite -Identity $SiteUrl 
$web=$site.RootWeb 
$MyContentType=$web.ContentTypes[$ContentName] 
$MyField=$MyContentType.Fields[$ColumnName] 
If($Required) 
{ 
    $MyContentType.FieldLinks[$MyField.Id].Required=$true 
} 
Else 
{ 
    $MyContentType.FieldLinks[$MyField.Id].Required=$False 
} 
$MyContentType.Update($true) 
Write-Host "The  required status of $ColumnName has been changed!"

Prerequisite

Windows PowerShell 2.0
Windows Server 2008R2

Additional Resources

Technical Resource:
Get-Spsite
http://technet.microsoft.com/en-us/library/ff607950.aspx