If you could accept not to use WebClient, you can use PowerShell CSOM to download the file and rename the file in one method.
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function DownloadAndRename-FileFromLibrary()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceFile,
[Parameter(Mandatory=$true)] [string] $TargetFile,
[Parameter(Mandatory=$true)] [string] $NewFileUrl
)
Try {
#Setup Credentials to connect
$username = "amos@contoso.onmicrosoft.com"
$password = "password"
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#if you are using SharePoint On premise,just delete the above Credentials script
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$SourceFile)
$WriteStream = [System.IO.File]::Open($TargetFile,[System.IO.FileMode]::Create)
$FileInfo.Stream.CopyTo($WriteStream)
$WriteStream.Close()
Write-host -f Green "File '$SourceFile' Downloaded to '$TargetFile' Successfully!" $_.Exception.Message
$File = $Ctx.Web.GetFileByServerRelativeUrl($SourceFile)
$File.MoveTo($NewFileURL, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
$Ctx.ExecuteQuery()
write-host -f Green "File Renamed successfully!"
}
Catch {
write-host -f Red "Error Downloading File!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://contoso.sharepoint.com/sites/dev/"
$SourceFile="/sites/dev/Shared Documents/1.pdf" #Relative URL
$TargetFile="C:\Temp\1.pdf"
$NewFileURL="/sites/dev/Shared Documents/2.pdf"
#Call the function to download file
DownloadAndRename-FileFromLibrary -SiteURL $SiteURL -SourceFile $SourceFile -TargetFile $TargetFile -NewFileUrl $NewFileURL
Reference:
https://www.sharepointdiary.com/2016/09/sharepoint-online-download-file-from-library-using-powershell.html
https://www.sharepointdiary.com/2017/10/sharepoint-online-rename-files-in-document-library-using-powershell.html
PNP powershell suggested by sharatha is also another solution.
PNP powershell installation
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.