1

I am using the following script in powershell to copy documents from a file directory to a Document library:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
     Add-PSSnapin Microsoft.SharePoint.PowerShell;
 }

$siteCollUrl = "http://sharepoint"
$libraryName = "Documents"
#Physical/Network location of files
$reportFilesLocation  = "C:\docs"

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

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

$files = ([System.IO.DirectoryInfo] (Get-Item $reportFilesLocation)).GetFiles()
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
    $fileStream.Close();
}
$spSourceWeb.dispose();
Write-Host "Files have been uploaded to $libraryName."

The created by / modified column are set by default as the user that runs the script, but every document (.doc/.pdf./etc) has the property 'owner'. How to extract this and set the appropriate column to this property?

1

You can get the owner of the file say test.txt - for a single file - using the below command:

Get-Acl D:\Scripts\Test.txt | Select-Object Owner

for multiple files within a folder/subfolder, you can use :

Get-ChildItem D:\Scripts -recurse | ForEach-Object {Get-Acl $_.FullName}

You can incorporate the above in your PS code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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