PowerShell script to update SharePoint list or library Columns based on file name in the CSV File.
Sharepoint Site
Input File
Name |
ECMSourcePath |
Abc.doc |
/SiteCollection/SubSite/Library/Raghav |
File1.pdf |
/SiteCollection/SubSite/Library/Sai |
File4.pdf |
/SiteCollection/SubSite2/Library/Test |
Script
- Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
- $Site= Get-SPSite "http://sharepoint2019/sites/RaghavSite"
- $CSV_Input = Import-Csv "C:\Raghav\Input.csv"
- $ItemSuccess = @()
- #Iterating the Site Collection
- foreach($Web in $Site.AllWebs)
- {
- #Iterating the Site Objects
- foreach($List in $Web.Lists)
- {
- #Condition to check only the document libraries and skipping the default libraries which created while sitecollections gets created.
- if(($List.BaseType -eq "DocumentLibrary") -and ($List.Title -ne "Pages") -and ($List.Title -ne "Web Part Gallery")
- -and ($List.Title -ne "Theme Gallery") -and ($List.Title -ne "Site Pages") -and ($List.Title -ne "Converted Forms")
- -and ($List.Title -ne "Master Page Gallery") -and ($List.Title -ne "SiteAssets")
- -and ($List.Title -ne "PublishingImages") -and ($List.Title -ne "SiteCollectionDocuments")
- -and ($List.Title -ne "Style Library") -and ($List.Title -ne "FormServerTemplates"))
- {
- #Iterating the Input csv file
- foreach ($Value in $CSV_Input)
- {
- #Fetching the List Item Title Field "Name" field in the CSV whereas internal Name is Title. change accordingly as per your field name
- $Item = $List.Items | Where-Object { $_['Name'] -eq $Value.Name }
- if($Item -ne $null)
- {
- #Setting ECMSourcePath Field Value from input file .
- $Item["ECMSourcePath"] = $Value.ECMSourcePath
- $ECMFolderUpdate=$Value.ECMSourcePath
- #ECMFolder Value is the last value before the '/' delimiter for extracting we use string functions.
- $ECMFolderUpdate=$ECMFolderUpdate.Substring($ECMFolderUpdate.lastIndexOf('/')).Replace('/' ,'')
- $Item["ECMFolder"] = $ECMFolderUpdate
- #To Avoid the Timestamp Values of Modified and Modified By here we using Item.SystemUpdate($false) you can also use Item.Update as well.
- $item.SystemUpdate($false)
- #Exporting the Update details to Output File.
- $itemdetail = New-Object System.Object
- $itemdetail | Add-Member -MemberType NoteProperty -Name "ListName" -Value $Item.ParentList
- $itemdetail | Add-Member -MemberType NoteProperty -Name "ItemName" -Value $Item.Name
- $itemdetail | Add-Member -MemberType NoteProperty -Name "ItemUrl" -Value $Item.Url
- $ItemSuccess +=$itemdetail
- Write-Host "Updated in the Site: " $web.Url "- library :" $List.Title "Item:" $Item.Name -ForegroundColor DarkCyan
- }
- }
- }
- }
- }
- $ItemSuccess | Export-CSV -Path "C:\Raghav\SuccessFile.csv" -NoTypeInformation -Force
- Write-Host "Script Completed" -foreground Green