0

Here is my document library looks like. I have 2 content types with my document library. 1 content type is based on Document and the other content type is based on "Link to a document" type.

I am trying to upload some files using powershell script. I am using a csv file to read line by line and upload files to document library. I am getting error on $i.fileNameASPX. Powershell tells me that Missing expression after , . So what to do.

if($i.ContentType = "LegalLink2Document")
{
    $itemType = $docLibrary.ContentTypes[$i.ContentType]
    $newFile = $docLibrary.RootFolder.Files.Add($i.fileNameASPX, UTF8Encoding.UTF8.GetBytes(builder.ToString()), $true) 
    $theItem = $newFile.Item
    $theItem["ContentTypeId"] = $itemType.Id
    $itemUrl = ew-object Microsoft.SharePoint.SPFieldUrlValue()
    $itemUrl.Url = $i.fileLinkUrl
    $itemUrl.Descrition = $i.URLDESC
    $theItem["URL"] = $itemUrl      
}
c0deNinja
  • 3,956
  • 1
  • 29
  • 45
torres
  • 1,283
  • 8
  • 21
  • 30
  • Is ur $itemUrl a typo? Suppose to be New-Object right?? – c0deNinja Jul 02 '12 at 20:18
  • Oh yeah. that too. but it's stuck at the $newfile the 4th line from the top. I think UTF8Enconding is supposed to be different format. – torres Jul 02 '12 at 22:04
  • 1
    This seems like a duplicate post to: http://stackoverflow.com/questions/11304002/is-it-easy-to-convert-about-15-lines-of-code-c-sharp-code-to-powershell I responded to the other post – Elijah W. Gagne Jul 03 '12 at 13:14

1 Answers1

2

Here is the Simple PowerShell script to upload files to a document library in SharePoint2013

http://soreddymanjunath.blogspot.in/2014/07/add-file-to-document-library-using.html

cls

asnp "*sh*"

$url=Read-Host "Enter Site Url" 

$web=Get-SPWeb -Identity $url

if($web)
{
try
{
$list = $web.Lists.TryGetList("Documents")

$files = Get-ChildItem -Path "D:\M" -Force -Recurse

    foreach ($file in $files)
    {
      $stream = $file.OpenRead()

      $done= $list.RootFolder.Files.Add($file.Name, $stream, $true)
       
      Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green

       

    }
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
}
}

 else
{
Write-Host "Site Doesn't exist"
 }

$list.Update();
Community
  • 1
  • 1