How to upload the file into the document library and insert/update the metadata columns.I am having 3 people picker data types/user group columns and one choice field and 2 single line of text columns. I have written the PowerShell to upload the document in the document library but how to get the item which is inserted and update the metadata. Do I need to use the getitembyid()? Also when I ran the script it inserted successfully and its checked-out.

        if((Get-PSSnapin | Where {$_.Name -
        eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
            Add-PSSnapin Microsoft.SharePoint.PowerShell;

           #Site Collection where you want to upload files
            = "http://srvr1:123/sites/R-Engineering/"
          #Document Library where you want to upload files
         $libraryName = "KG"
         #Physical/Network location of files
          $reportFilesLocation  = "E:\Maddy\Scripts\FileUpload"

        $spSourceWeb = Get-SPWeb $siteCollUrl;
        $spSourceList = $spSourceWeb.Lists[$libraryName];

            if($spSourceList -eq $null)
           Write-Host "The Library $libraryName could not be found."

   $files = ([System.IO.DirectoryInfo] (Get-Item 
  foreach($file in $files)
    #Open file
   $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

   #Add file
   $folder =  $spSourceWeb.getfolder($libraryName)

   Write-Host "Copying file $file to $libraryName..."
   $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name,   
   [System.IO.Stream]$fileStream, $true)

   #Close file stream
Write-Host "Files have been uploaded to $libraryName."

1 Answer 1


Right after you add the file:

$item = $spFile.Item;
$item["TextOrChoiceColumnInternalName"] = "New Value"
$spUser = Get-SPUser -Identity "domain\username" -Web $spSourceWeb;
$item["UserFieldInternalName"] = $spUser;

Use SystemUpdate($false) if you don't want to increase the version of the item..

To check in a file use:

$spFile.CheckIn("Checked In By Administrator")

To publish a file:

$spFile.Publish("Automatically published by Powershell")

To approve a file:

$spFile.Approve("Automatically approved by by Powershell");
  • Thanks. Its checked out now.How can I check in this item & publish through PowerShell.
    – samolpp2
    May 4, 2015 at 5:56
  • Edited my answer. May 5, 2015 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.