Downloading File from SharePoint Online Using PowerShell, OAuth and Invoke-Rest Method and Downloaded File is not correctly Formatted

With the below code, I am able to successfully authenticate to the site and download a file from SharePoint Online but the file appears to have a bunch of ancillary information from the site and not the actual text or images that it should have. I am able to successfully download the file using Postman but from Powershell, using Invoke-RestMethod, it appears to not format the file correctly after saving to local disk. Do I need to add a parameter to get it to save the file correctly?

(BEGIN CODE)

$filename = ‘test1File.pdf’
$directory = ‘C:temp’
$path = $directory + $filename
$outfile = “C:temp$filename”

Function GetAccessToken() {

$appId = ‘d78f1248-6516-4ac5-838a-4a42c049435b’ #This is the Client ID
$appSecret = ‘(App Secret Here)’ #This is the Client Secret
$principal = ‘00000003-0000-0ff1-ce00-000000000000’ #This is the SharePoint Principal ID
$realm = ‘(Tenant ID Here)’ #This is the Tenant Id
$targetHost = ‘https://xxxxxx.sharepoint.com/sites/xxxx’ #This is the SharePoint Site
$resource = “00000003-0000-0ff1-ce00-000000000000/xxxxxx.sharepoint.com@(Tenant ID Here)”

$body = (ordered)@{
grant_type = ‘client_credentials’
client_id = “$appId@$realm”
client_secret = $appSecret
resource = $resource
}

$accessTokenUrl = “https://accounts.accesscontrol.windows.net/$realm/tokens/OAuth/2”
$global:Access_Token = Invoke-RestMethod -uri $accessTokenUrl -Body $body -Method Post

Write-Host “Token =” $global:Access_Token.Access_token

}

Function DownloadFile() {

$url = “https://adponline.sharepoint.com/sites/ecm-poc/_api/web/GetFolderByServerRelativeUrl(‘/sites/ecm-poc/Documents’)/Files(‘$filename’)/$value”
$headers = @{‘Authorization’ = “Bearer $access_token”;’Content-Type’=”application/json”;’Accept’=”application/json;odata=verbose”}
$DownloadFile = Invoke-RestMethod -Uri $url -Headers $headers -Method Get -OutFile $outfile -Verbose
}

GetAccessToken

$access_token = $global:Access_Token.Access_token

DownloadFile

#Clear-Host
Write-Host “File Downloaded Successfully!”

(END CODE)

Below is a Snipit of what it looks like after downloading the file:
*/filename.txt’)/Versions”}},”CheckInComment”:””,”CheckOutType”:2,”ContentTag”:”{ECB1B9A7-6774-467F-9A06-C91020BE52DD},2,1″,”CustomizedPageStatus”:0,”ETag”:””{ECB1B9A7-6774-467F-9A06-C91020BE52DD},2″”,”Exists”:true,”IrmEnabled”:false,”Length”:”215″,”Level”:1,”LinkingUri”:null,”LinkingUrl”:””,”MajorVersion”:1,”MinorVersion”:0,”Name”:”CarTires3.txt”,”ServerRelativeUrl”:”/sites/ecm-poc/Documents/CarTires3.txt”,”TimeCreated”:”2021-07-02T19:29:25Z”,”TimeLastModified”:”2021-07-02T19:29:25Z”,”Title”:null,”UIVersion”:512,”UIVersionLabel”:”1.0″,”UniqueId”:”ecb1b9a7-6774-467f-9a06-c91020be52dd”}}