Add a column to multiple document libraries

KOB 0 Reputation points
2023-02-01T16:20:08.9033333+00:00

Hello comunity,
I want to add a columns to a number of document libraries at the same time in my sharepoint site. Essentially I have 50+ document libraries set up and I now need to add a new column to all of them, is there a powershell script to do this instead of doing it manualy ?
Thanks for help.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,595 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,040 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Emily Du-MSFT 41,771 Reputation points Microsoft Vendor
    2023-02-02T07:59:47.9+00:00

    1.Create a csv file as following picture shows. Enter document library title in the csv file.User's image

    2.Please run below PowerShell.

    $DisplayName = "testtextcolumn"
    $InternalName = "testtextcolumn" 
    
    $Credential = Get-Credential
    Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/emilytestnew" -Credential $Credential
    
    $filePath = “C:\Users\spadmin\Desktop\dl.csv”  
    
    $csv = Import-Csv $filePath  
    $a = $csv.Title
      
    ForEach($_ in $a) {  
    
        Add-PnPField -List $_ -DisplayName $DisplayName -InternalName $InternalName -Type Text -AddToDefaultView
    
    } 
    

    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.


  2. Limitless Technology 43,926 Reputation points
    2023-02-02T17:30:28.62+00:00

    Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query.

    You can use PowerShell to automate adding a column to multiple document libraries. First, you will need to create the column and set its properties. You can use the PowerShell command New-SPField to do this. After creating the column, you can use the command Add-SPFieldToList to add the column to a single library.

    To add the column to multiple libraries, you will need to use a foreach loop. You can use the command Get-SPWeb to get all the document libraries in your SharePoint site. Then, you can use the foreach loop to loop through each library, and use the Add-SPFieldToList command to add the new column to each library.

    For example:

    Get all the libraries in the site

    $libraries = Get-SPWeb -Limit ALL | Select-Object -ExpandProperty Lists

    Loop through each library

    foreach($library in $libraries)

    {

    # Add the new column to the current library
    
    Add-SPFieldToList -List $library -Field "NewColumnName"
    

    }

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.

    0 comments No comments