Share via

Add a column to multiple document libraries

KOB 5 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.

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Limitless Technology 45,226 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.

    1 person found this answer helpful.
    0 comments No comments

  2. Emily Du-MSFT 51,986 Reputation points Microsoft External Staff
    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.


Your answer

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