SharePoint Online: Get-Set “Yes/No (Check box)” Field Value using PowerShell

Requirement: Get or Set Yes/No Field Value in SharePoint Online using PowerShell.

In SharePoint Online, the “Yes/No (check box)” fields allow users to indicate a selection. This can be helpful when you need to track boolean values such as “status” from users. You can set the value of this field or retrieve its value using PowerShell. In this post, we will show you how to use PowerShell to get and set the value of a “Yes/No” field.

yes no field sharepoint online powershell

PowerShell to Get Value of a Yes/No Field in SharePoint Online:

Here is how you can read the Yes/No field programmatically using PowerShell.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Set parameter values accordingly
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="IsActive" #Internal Name
$ListItemID="5"
 
#Get Credentials to connect
$Cred= Get-Credential
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Get the List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the Yes/No Field Value
$ListItem[$FieldName]

Update Yes/No Field value in SharePoint Online List using PowerShell

Let’s update the Yes/No field in the SharePoint Online list programmatically.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="IsActive" #Internal Name
$ListItemID="5"
 
#Get Credentials to connect
$Cred= Get-Credential
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Get the List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Update the Yes/No Field Value
$ListItem[$FieldName] = $True
$ListItem.update()
$Ctx.ExecuteQuery()

Write-host -f Green "Field Value Updated!"

PnP PowerShell to Update “Yes/No” Field Values in SharePoint Online

To set the value for “Yes/No” boolean fields, use Values @{“YesNoFieldInternalName” = $True/$false} through the Set-PnPListItem cmdlet. Here is an example:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$FieldName = "IsActive"
$ItemID = 1

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Update Yes/No Field Value
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = $True}

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *