none
Sharepoint 2013 powershell script to add list item attachment name to a single line field RRS feed

  • Question

  • Hi,

    i am in need of a powershell script to add the items attachment name (always only 1 attachment) to the list items normal single line column, has anyone done this and can give pointers?

    Friday, March 16, 2018 11:26 AM

Answers

  • Hi,

    The following PowerShell script for your reference:

    $webUrl = "http://sp2013/sites/team/"
    $listname = "CL15"
    $fieldname="Field1"
    $web=Get-SPWeb $webUrl
    $list = $web.Lists[$listname]
    
    foreach ($listItem in $list.Items)
    {
    	if($listItem.Attachments.Count -gt 0){
    		$attachment=$listItem.Attachments[0];
    		$listItem[$fieldname]=$attachment
    		$listItem.Update()
    	}
    }
    

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Fazzzer Monday, March 19, 2018 12:12 PM
    Monday, March 19, 2018 6:40 AM
    Moderator

All replies

  • hello, check the following links:
    Get attachment with element ID from list
    - how to retrieve attachments for list items via PowerShell
    Updating Sharepoint List items
    - how to update list items via PowerShell
    You need to combine them: at first iterate through list items and retrieve attachmens and for each list item store attachment name to the list item's field.


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - https://github.com/sadomovalex/camlex

    Friday, March 16, 2018 12:11 PM
  • I am getting errors when i try to update the list item with powershell:

    Error while executing PowerShell Script: Cannot index into a null array.



    • Edited by Fazzzer Friday, March 16, 2018 12:47 PM
    Friday, March 16, 2018 12:47 PM
  • Hi fazzzer,

    here's a powershell script for updating list item's title by attachment file's name

    Add-PSSnapin "microsoft.sharepoint.powershell"
    $fileAttachmentUrl = "http://spwf/Shared%20Documents/31729627.doc"
    $web=get-spweb "http://spwf"
    $spFile = $web.GetFile($fileAttachmentUrl)
    $spListItem=$spFile.Item;
    $spListItem["Title"]=$spFile.Name;
    $spListItem.Update();
    Write-Host $spFile.Name

    here's the result


    Best Regrads, Ahmed Madany MCTS @twitter http://twitter.com/ahmed_madany @Blog http://ahmedmadany.wordpress.com @LinkedIn http://eg.linkedin.com/pub/ahmed-madany/35/80/2b6


    Friday, March 16, 2018 5:38 PM
  • Hi,

    The following PowerShell script for your reference:

    $webUrl = "http://sp2013/sites/team/"
    $listname = "CL15"
    $fieldname="Field1"
    $web=Get-SPWeb $webUrl
    $list = $web.Lists[$listname]
    
    foreach ($listItem in $list.Items)
    {
    	if($listItem.Attachments.Count -gt 0){
    		$attachment=$listItem.Attachments[0];
    		$listItem[$fieldname]=$attachment
    		$listItem.Update()
    	}
    }
    

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Fazzzer Monday, March 19, 2018 12:12 PM
    Monday, March 19, 2018 6:40 AM
    Moderator
  • Thanks, works perfectly

    Monday, March 19, 2018 12:07 PM