I have document library. I want to replace some document properties using powershell. There are multiple properties associated with each document. First I'll need to check for all documents if that document has that properties (metadata) and if yes need to replace that with new one. Any ideas? scripts?
2 Answers
here is powershell script to list all properties and then how to change them.
# Load SharePoint library
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
# Connect to the site collection http://SP2010 and store the object in the $site variable
$site = New-Object Microsoft.SharePoint.SPSite("http://SP2010")
# Connect to the root site in the site collection and store the object in $root
$root = $site.rootweb
# Store the Shared Documents document library in a variable $Docs
$docs = $root.lists["Shared Documents"]
# Display all the documents, their titles, names and IDs
$docs.items | format-table -property title,name,id
# Updates the title for each item in the list with Name
$docs.items | ForEach { $_["Title"] = $_["Name"]; $_.Update() }
# Display all the documents, their titles, names and IDs
$docs.items | format-table -property title,name,id
Try the following script:
$web = Get-SPWeb http://YourServer/ListLocation
$list = $web.Lists["TheList"]
foreach ($item in $list.Items)
{
if( $list.Fields.ContainsField("ddlTest") -eq $true)
{
$item["Number"] = Get-Random -Min 0 -Max 100;
$item.Update();
}
}