This script could be used to add attachments for a specific list item in Microsoft SharePoint Server 2010.
In a real world, IT Administrators may need to add attachments for a specific list item. It will be a heavy task if we do it manually, so the IT Administrators need a script to complete this task.
This script contains two advanced function, Add-OSCSPListItemAttachment and Remove-OSCSPListItemAttachment. You can use this script in following ways:
Method 1:
Method 2:
Function Add-OSCSPListItemAttachment
{
[CmdletBinding()]
Param
(
#Define parameters
[Parameter(Mandatory=$true,Position=1)]
[string]$SiteURL,
[Parameter(Mandatory=$true,Position=2)]
[string]$ListName,
[Parameter(Mandatory=$true,Position=3)]
[int]$ItemID,
[Parameter(Mandatory=$true,Position=4,ValueFromPipeline=$true)]
[string[]]$Attachment
)
Process
{
Try
{
#Use Get-SPWeb to get a SPWeb object
$spWeb = Get-SPWeb -Identity $SiteURL -ErrorAction Stop -Verbose:$false
}
Catch
{
$pscmdlet.WriteError($Error[0])
}
if ($spWeb -ne $null) {
#Try to get a list, if list name is wrong, this function will be terminated.
$spList = $spWeb.Lists.TryGetList($ListName)
if ($spList -eq $null) {
$errorMsg = $Messages.CannotFindSpecifiedList
$errorMsg = $errorMsg -replace "Placeholder01",$ListName
$customError = New-OSCPSCustomErrorRecord `
-ExceptionString $errorMsg `
-ErrorCategory ResourceUnavailable -ErrorID 1 -TargetObject $pscmdlet
$pscmdlet.WriteError($customError)
$spWeb.Dispose()
return $null
}
#Check attachments setting, if this feature is disabled,
#exits this fuction immediately.
if (-not $spList.EnableAttachments) {
$errorMsg = $Messages.AttachmentFeatureDisabled
$customError = New-OSCPSCustomErrorRecord `
-ExceptionString $errorMsg `
-ErrorCategory Notspecific -ErrorID 1 -TargetObject $pscmdlet
$pscmdlet.WriteError($customError)
$spWeb.Dispose()
return $null
}
#Try to get a list item by ID.
Try
{
$spListItem = $spList.GetItemByID($ItemID)
}
Catch
{
$errorMsg = $Messages.CannotFindSpecifiedItem
$customError = New-OSCPSCustomErrorRecord `
-ExceptionString $errorMsg `
-ErrorCategory ObjectNotFound -ErrorID 1 -TargetObject $pscmdlet
$pscmdlet.WriteError($customError)
}
if ($spListItem -ne $null) {
foreach ($attachmentPath in $Attachment) {
#Verify attachment path
$isExist = Test-Path -Path $attachmentPath -PathType Leaf
if ($isExist) {
$fileBits = [System.IO.File]::ReadAllBytes($attachmentPath)
#Exclude empty files
if ($fileBits.Length -ne 0) {
Try
{
$verboseMsg = $Messages.AddAttacment
$verboseMsg = $verboseMsg -replace "Placeholder01",$attachmentPath
$pscmdlet.WriteVerbose($verboseMsg)
$spListItem.Attachments.AddNow($attachmentPath,$fileBits) | Out-Null
}
Catch
{
$pscmdlet.WriteError($Error[0])
}
} else {
$warningMsg = $Messages.CannotAddEmptyFile
$warningMsg = $warningMsg -replace "Placeholder01",$attachmentPath
$pscmdlet.WriteWarning($warningMsg)
}
} else {
$warningMsg = $Messages.CannotFindSpecifiedAttachment
$warningMsg = $warningMsg -replace "Placeholder01",$attachmentPath
$pscmdlet.WriteWarning($warningMsg)
}
}
}
$spWeb.Dispose()
return $null
}
}
}
Function Add-OSCSPListItemAttachment { [CmdletBinding()] Param ( #Define parameters [Parameter(Mandatory=$true,Position=1)] [string]$SiteURL, [Parameter(Mandatory=$true,Position=2)] [string]$ListName, [Parameter(Mandatory=$true,Position=3)] [int]$ItemID, [Parameter(Mandatory=$true,Position=4,ValueFromPipeline=$true)] [string[]]$Attachment ) Process { Try { #Use Get-SPWeb to get a SPWeb object $spWeb = Get-SPWeb -Identity $SiteURL -ErrorAction Stop -Verbose:$false } Catch { $pscmdlet.WriteError($Error[0]) } if ($spWeb -ne $null) { #Try to get a list, if list name is wrong, this function will be terminated. $spList = $spWeb.Lists.TryGetList($ListName) if ($spList -eq $null) { $errorMsg = $Messages.CannotFindSpecifiedList $errorMsg = $errorMsg -replace "Placeholder01",$ListName $customError = New-OSCPSCustomErrorRecord ` -ExceptionString $errorMsg ` -ErrorCategory ResourceUnavailable -ErrorID 1 -TargetObject $pscmdlet $pscmdlet.WriteError($customError) $spWeb.Dispose() return $null } #Check attachments setting, if this feature is disabled, #exits this fuction immediately. if (-not $spList.EnableAttachments) { $errorMsg = $Messages.AttachmentFeatureDisabled $customError = New-OSCPSCustomErrorRecord ` -ExceptionString $errorMsg ` -ErrorCategory Notspecific -ErrorID 1 -TargetObject $pscmdlet $pscmdlet.WriteError($customError) $spWeb.Dispose() return $null } #Try to get a list item by ID. Try { $spListItem = $spList.GetItemByID($ItemID) } Catch { $errorMsg = $Messages.CannotFindSpecifiedItem $customError = New-OSCPSCustomErrorRecord ` -ExceptionString $errorMsg ` -ErrorCategory ObjectNotFound -ErrorID 1 -TargetObject $pscmdlet $pscmdlet.WriteError($customError) } if ($spListItem -ne $null) { foreach ($attachmentPath in $Attachment) { #Verify attachment path $isExist = Test-Path -Path $attachmentPath -PathType Leaf if ($isExist) { $fileBits = [System.IO.File]::ReadAllBytes($attachmentPath) #Exclude empty files if ($fileBits.Length -ne 0) { Try { $verboseMsg = $Messages.AddAttacment $verboseMsg = $verboseMsg -replace "Placeholder01",$attachmentPath $pscmdlet.WriteVerbose($verboseMsg) $spListItem.Attachments.AddNow($attachmentPath,$fileBits) | Out-Null } Catch { $pscmdlet.WriteError($Error[0]) } } else { $warningMsg = $Messages.CannotAddEmptyFile $warningMsg = $warningMsg -replace "Placeholder01",$attachmentPath $pscmdlet.WriteWarning($warningMsg) } } else { $warningMsg = $Messages.CannotFindSpecifiedAttachment $warningMsg = $warningMsg -replace "Placeholder01",$attachmentPath $pscmdlet.WriteWarning($warningMsg) } } } $spWeb.Dispose() return $null } } }
Example 01: Displays help about Add-OSCSPListItemAttachment
Command: Get-Help Add-OSCSPListItemAttachment -Full
Screenshot:

Example 02: Add an attachment for a specific list item.
Command:
Add-OSCSPListItemAttachment -SiteURL "http://server_name/sites/sitename" -ListName "ListName" -ItemID 1 -Attachment "c:\scripts\test00.txt" -Verbose
Screenshot:
![]()
Example 03: Add multiple attachments for a specific list item. (Method 01)
Command:
Add-OSCSPListItemAttachment -SiteURL "http://server_name/sites/sitename" -ListName "ListName" -ItemID 1 -Attachment "c:\scripts\test02.txt","c:\scripts\test03.txt" -Verbose
Screenshot:
![]()
Example 04: Add multiple attachments for a specific list item. (Method 02)
Command:
$files = Get-ChildItem c:\Scripts -Filter "Test1*.txt"
$files | %{Add-OSCSPListItemAttachment -SiteURL "http://server_name/sites/sitename" -ListName "ListName" -ItemID 1 -Attachment $_.FullName -Verbose}
Screenshot:

Example 05: Recycle attachments which name matches Test1[0|2|4|6|8], like test10.txt, test12.txt, test14.txt, test16.txt, test18.txt. Other regular expression is supported. If you want to remove single item, please specify the full name,
like test10.txt.
Command:
Remove-OSCSPListItemAttachment -SiteURL "http://server_name/sites/sitename" -ListName "ListName" -ItemID 1 -AttachmentName "Test1[0|2|4|6|8]" -MoveToRecycleBin -Verbose
Screenshot:
Example 06: Remove attachments which name matches Test1[1|3|5|7|9], like test11.txt, test13.txt, test15.txt, test17.txt, test19.txt. Other regular expression is supported. If you want to remove single item, please specify the full name,
like test10.txt.
Command:
Remove-OSCSPListItemAttachment -SiteURL "http://server_name/sites/sitename" -ListName "ListName" -ItemID 1 -AttachmentName "Test1[0|2|4|6|8]" -MoveToRecycleBin:$false -Verbose
Screenshot:

Technical Resource:
Windows PowerShell Advanced Function
http://technet.microsoft.com/en-us/library/dd315326.aspx
Microsoft.SharePoint.SPSite Class
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.aspx
Microsoft.SharePoint.SPList Class
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.aspx
Microsoft.SharePoint.SPAttachmentCollection Class
http://msdn.microsoft.com/en-us/library/ms472013.aspx
Forum Threads:
Adding list attachment with powershell
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/4ef53940-6a79-4760-acaa-6641f3fda626
PowerShell to add attachments to an item
http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/36297c58-d066-4a6d-a880-aea590cecc07