'Powershell / Chrome Selenium - Forcing update of default download directory

I have been trying out some automation using Powershell and Selenium + Chrome, however I am struggling to be able to have the default download folder actually apply when Chrome is launched.

I have used this as a guide to set the default download directory in Powershell, and while it does boot launch Chrome with the directory set in "chrome://settings/?search=download", it isn't actually applied (unless you manually press the 'Change' button in settings, which defeats the point of automating it!) and still download to the default user downloads folder:

Chrome settings after PS launch

#WebDriver.dll - C# 4.10 - .NET 4.8
#ChromeDriver - 97.0.4692.71 

$WorkingPath = 'C:\PSChromeSelenium'
[System.Reflection.Assembly]::LoadFrom("$($workingPath)\WebDriver.dll")

$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions

$SavePath="C:\Temp"
$HashTable = @{}
$HashTable.Add("prompt_for_download", $true)
$HashTable.Add("default_directory", $SavePath)

$ChromeOptions.AddUserProfilePreference("download", $HashTable)

$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeOptions)

Downloading using 'Save / Save As' still opens to the default folder.

Any help would be appreciated! Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source