0

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"
Compo
  • 36,585
  • 5
  • 27
  • 39
silva2021
  • 19
  • 5
  • 4
    `start-process -FilePath ('chrome.exe','firefox.exe'|get-random) "www.quora.com"`. probably this works, if not use the full path of both browsers. – Santiago Squarzon Apr 10 '21 at 20:00
  • @santiago-squarzo Is there a way to randomize the browsers for Batch-file as well? – silva2021 Apr 10 '21 at 23:31
  • You can use `%RANDOM%` in a batch file to do random things. You should try to write a solution, and if you run into problems, ask a new question showing what you've done and asking for helping resolving the problems. – Anon Coward Apr 11 '21 at 00:25
  • @anon-coward Hey, I tried and those are the results I found which ends with error. Thanks for your answer though. – silva2021 Apr 11 '21 at 08:28

1 Answers1

1

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:

Mofi
  • 46,139
  • 17
  • 80
  • 143