Forum Discussion

Rafia25000's avatar
Rafia25000
Copper Contributor
Nov 08, 2022

Setting DefaultColumnValue using PnP powershell Script with "&&" or " ' " Symbol

Hi people! I am trying to set my choice column default value as "Folder with &" the script I am using is working fine for simple Text with no special characters but when it comes to this particular character it skips setting up the default column value for that folder 

 

$FolderList1 = Get-PnPFolderItem -FolderSiteRelativeUrl $DestinationLibraryName -ItemType Folder

foreach ($item in $FolderList1)
{
echo $item.Name
}

#Show the information of List
$List = Get-PnPList -Identity $DestinationLibraryName -Includes RootFolder.Folders

#Get All the FoldersPath in the List
$RootFolderURL = $List.RootFolder.Folders.ServerRelativeUrl
#Get the List items information
$ListItems = Get-PnPListItem -List $DestinationLibraryName
$Folders = $ListItems | Where {$_.FileSystemObjectType -eq "Folder"}

#Set the Default Value for Content Type Column level 1
ForEach($item in $FolderList1)
{

echo $DestinationLibraryName
echo $item.Id
echo $item.Name

Set-PnPDefaultColumnValues -List $DestinationLibraryName -Value $item.Name -Folder $item.Name -Field "ClientDocumentCategory"
}


# Set the Default Value for Content Type Column level 2
ForEach($item in $FolderList1)
{
$Level1Folder = $item.Name
$Level1folderPath = $DestinationLibraryName +"/"+ $Level1Folder

$Level1FolderList = Get-PnPFolderItem -FolderSiteRelativeUrl $Level1folderPath -ItemType Folder

Foreach($itemLevel1 in $Level1FolderList)
{
$ColumnValue = $itemLevel1.Name
$Path = $Level1folderPath
$L2FOLDER = $Level1Folder+ "/" + $ColumnValue

Set-PnPDefaultColumnValues -List $Path -Value $ColumnValue -Folder $L2FOLDER -Field "ClientDocumentType"

}
}

 

  • To set the default value of a choice column with special characters like "&" in PowerShell, you need to use escape characters. In PowerShell, the escape character is the backtick (`), which can be used to escape special characters.

    So, to set the default value of a choice column to "Folder with &", you can modify your script like this:

    # Set the Default Value for Content Type Column level 1
    ForEach($item in $FolderList1) {
        $defaultValue = "Folder with `&"
        Set-PnPDefaultColumnValues -List $DestinationLibraryName -Value $defaultValue -Folder $item.Name -Field "ClientDocumentCategory"
    }
    
    # Set the Default Value for Content Type Column level 2
    ForEach($item in $FolderList1) {
        $Level1Folder = $item.Name
        $Level1folderPath = $DestinationLibraryName + "/" + $Level1Folder
    
        $Level1FolderList = Get-PnPFolderItem -FolderSiteRelativeUrl $Level1folderPath -ItemType Folder
    
        Foreach($itemLevel1 in $Level1FolderList) {
            $defaultValue = "Folder with `&"
            $ColumnValue = $itemLevel1.Name
            $Path = $Level1folderPath
            $L2FOLDER = $Level1Folder+ "/" + $ColumnValue
    
            Set-PnPDefaultColumnValues -List $Path -Value $defaultValue -Folder $L2FOLDER -Field "ClientDocumentType"
        }
    }

    Note that we're using the backtick (`) to escape the "&" character in the default value variable.

Resources