How can I create multiple site columns in SharePoint Online using PowerShell?

Stephanie Dury 0 Reputation points
2023-10-09T07:02:33.6866667+00:00

Hi all, I need to create 64 site columns (not list columns) using PowerShell. I want these columns created at the Content Type Gallery level so I can reuse these columns across sites within our tenancy. They will mostly be text, choice, number, currency and people columns that I need to create.

I've had a search for similar problems but can only find the script and csv template for creating list columns, not site columns. My PowerShell experience is limited and so I don't know if it would be a case of removing the $ListName line from the script and "hey-presto, it works!". Something tells me there's a bit more involved.

I'm trying to find a .csv file template where I can include the display name, internal name, type of column, description of column, default value, whether it's required or not, the group the column should be created in and "other" to add columns choices for the "Choice" columns. And ultimately the script I'll need to run in PowerShell to get these site columns created.

If this has already be asked (and answered), I do apologise. Sharing the link would be great! Thank you.

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2023-10-10T06:51:30.0033333+00:00

    Hi @Stephanie Dury ,

    You can use PowerShell to create site columns in batches, but due to the differences in each column type, the creation will be somewhat different. Regarding your needs, I tested it and found that the people type site column cannot be created together:

    #Config Variables
    $SiteURL = "https://tenant.sharepoint.com/sites/sitename/"
    
    $file = "C:\csvfile.csv" 
    
    #Get the CSV file
    $csv = Import-CSV $file
    
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive  
    
    ForEach($line in $csv)
    {
     #write-host $line.ColumnTitle   
    Try {
             #Create Site Column
             Add-PnPField -DisplayName $line.ColumnTitle -InternalName $line.InternalName -Group $line.ColumnGroup -Type $line.ColumnType
    }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
    }
    

    10101

    This blog contains all the site column types you need for your reference:

    https://www.sharepointdiary.com/2016/12/sharepoint-online-create-site-column-using-powershell.html

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.