# Import the SharePoint Online PowerShell module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
# Connect to SharePoint Online
Connect-SPOService -Url "https://yourtenant-admin.sharepoint.com/"
# Set the variables for the file and SharePoint library
$FileName = "file.txt"
$LibraryName = "Documents"
$SiteURL = "https://yourtenant.sharepoint.com/sites/YourSite"
# Get the SharePoint library
$Library = Get-SPOList -Identity $LibraryName -Web $SiteURL
# Get the file content
$FileStream = [System.IO.File]::OpenRead($FileName)
# Upload the file to SharePoint
$File = Add-SPOFile -Path $FileName -Folder $Library.RootFolder -Stream $FileStream
# Close the file stream
$FileStream.Close()
In this script, you'll need to replace "yourtenant" and "YourSite" with your
SharePoint Online tenant and site URL, respectively. You'll also need to replace
"file.txt" with the name of the file you want to upload, and "Documents" with
the name of the SharePoint document library you want to upload the file to.