Hi Mario2286-5314,
Invoke-Sqlcmd -ServerInstance $_ -Database $database.name -InputFIle $filepath | Out-File -FilePath $outfile -Append
It seems you have use sqlcmd in the Powershell SQLPS module. Please refer to Invoke-Sqlcmd which might help. And it seems it is a known issue that memory usage is high when using invoke-sqlcmd in the Powershell. Please refer to PowerShell Invoke-SqlCmd and memory usage and Creating large CSVs -- running into issue with RAM usage which might help.
You also can try to use command Invoke-DbaQuery in the Powershell dbatools.
Best Regards,
Amelia
If the answer 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.