Search code examples
powershellfilegoogle-chromelocal

Open local file with query params (Using Chrome and Powershell)


I need to launch a local application (it needs some parameters in the query string) with Chrome and Powershell.

This is what I'm trying, but it doesn't work.

$MediaName = "Test_Media"

$CurrentLocation = Get-Location

$PreviewTemplateLocation = "$($CurrentLocation.Drive.Name):\\Work\_all\Tools\Script\MCD\Script\template"
$TemplateIndexLocation = Join-Path -Path $PreviewTemplateLocation -ChildPath "index.html"

[System.Diagnostics.Process]::Start("chrome.exe", "$($TemplateIndexLocation)`#media=$($MediaName) --incognito --disable-web-security --auto-open-devtools-for-tabs --user-data-dir=C:/chromeTemp")

The url should be

C:\Work_all\Tools\Script\MCD\Script\template\index.html#media=Test_Media

but what I get is

C:\Work_all\Tools\Script\MCD\Script\template\index.html%23media=Test_Media

Note that I get %23 instead of # in the URL. Shouldn't the # character be escaped or something like that?

Thank you!!


Solution

  • If you use a regular file-system path, Chrome automatically escapes URL metacharacters such as # for you (%23 is the escaped form of #).

    Use the file:// protocol to avoid this escaping (file:/// to specify a local path).
    Additionally, you can use Start-Process instead of [System.Diagnostics.Process]::Start(), for a more PowerShell-idiomatic experience.

    Start-Process chrome "file:///$TemplateIndexLocation#media=$MediaName --incognito --disable-web-security --auto-open-devtools-for-tabs --user-data-dir=C:/chromeTemp"