I have this PowerShell command line which usually opens a webpage via Google Chrome, but I would like that to open randomly with Chrome and Firefox.
start-process -FilePath chrome.exe "www.quora.com"
I have this PowerShell command line which usually opens a webpage via Google Chrome, but I would like that to open randomly with Chrome and Firefox.
start-process -FilePath chrome.exe "www.quora.com"
One PowerShell solution for usage in a PowerShell script or in PowerShell console window is as posted by Santiago Squarzon:
start-process -FilePath ('chrome.exe','firefox.exe'|get-random) "https://www.quora.com"
For understanding the two used PowerShell cmdlets and how they work, open a PowerShell console window, execute there the following commands, and read entirely the two help pages displayed for the two cmdlets very carefully.
help start-process
help get-random
One batch file solution is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set /A "Browser=%RANDOM% & 1"
if %Browser% == 0 (set "Browser=chrome") else set "Browser=firefox"
start "" %Browser%.exe "https://www.quora.com"
endlocal
The first two command lines just define the required execution environment completely and creates a new environment variables list as copy of the current environment variables list and the last line just results in restoring the initial execution environment and environment variables list. The second and the last command line could be omitted if it is no problem to depend on Windows defaults or what the process calling this batch file defines as execution environment.
The third line uses an arithmetic expression to apply on a random number a bitwise AND with 1
to get assigned to the environment variable Browser
randomly either 0
or 1
.
The third line could be also:
set /A Browser=%RANDOM% %% 2
This arithmetic expression divides a random number by two and gets the remainder 0
or 1
assigned to the environment variable Browser
.
The fourth line uses an IF condition to redefine the environment variable Browser
on having currently the value 0
with the string chrome
and otherwise with the string firefox
.
The fifth line uses command start
to start either chrome.exe
or firefox.exe
as separate process on which cmd.exe
does not wait for self-termination before continuation of processing of the batch file with passing the URL to started executable. ""
defines an empty string as optional title for the console window not opened at all as the two browsers are Windows applications with a graphic user interface.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
help echo
or echo /?
help endlocal
or endlocal /?
help if
or if /?
help set
or set /?
help setlocal
or setlocal /?
help start
or start /?
See also:
powershell.exe
does the same. PowerShell and CMD use the Windows kernel library function CreateProcess to start the Chrome or Firefox process.