title: Uploading documents to Sharepoint with Powershell -
body: <p>The script exports to a CSV and the files need to be uploaded to SharePoint online.</p>
  
  <ol>
  <li>The export works perfectly </li>
  <li>Document is renamed to exclude "@mycompany.com"</li>
  <li>The error I get is : </li>
  </ol>
  
  <p>Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
  At line:46 char:9
  +         $Context.ExecuteQuery()
  +         ~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException</p>
  
  <pre><code>   Get-ChildItem 'c:\users\xxxx\downloads' -Filter *.csv | % {
  
          $eachFile =Get-ChildItem $Outputfile -Filter *.csv
  
          $eachFile | ForEach-Object{
  
          #Upload Variables  - Admin Heidi
          [string]$AdminName = "hxxn@xxx.onmicrosoft.com"
          [string]$AdminPassword = "Cxx1"
  
          #Upload Credentials 
          [SecureString]$SecurePass = ConvertTo-SecureString $AdminPassword -AsPlainText -Force 
          $credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $(convertto-securestring $AdminPassword -asplaintext -force)
          $AdminSiteURL="https://nrcl.sharepoint.com"
  
          #Site report will be stored on SharePoint
          $SiteURL="https://xxx.sharepoint.com/sites/nrg/"
          $LibraryName="Audit History"
  
          #Set up the context
          $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
          $Context.Credentials = $Credentials
  
          #Get the Library
          $Library =  $Context.Web.Lists.GetByTitle($LibraryName)
  
          #Get the file from c:/
          $FileStream = ([System.IO.FileInfo] (Get-Item $OutputFile)).OpenRead()
  
          #Get File Name from source file path
          $SourceFileName = Split-path $eachFile -leaf
  
           #sharepoint online upload file powershell
          $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
          $FileCreationInfo.Overwrite = $true
          $FileCreationInfo.ContentStream = $FileStream
          $FileCreationInfo.URL = $SourceFileName
          $FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
  
          #powershell upload single file to sharepoint online
          $Context.Load($FileUploaded)
  #######--------fails after this line  ---------------#####
          $Context.ExecuteQuery()
  
          #Close file stream
          $FileStream.Close()
  
          write-host "File has been uploaded!"
  
          }
  
  
  </code></pre>