11

I am trying to use a programming language to search google or another specified search engine. I would like to use windows cmd prompt to do so because the specified programming language has a simple command to access the cmd prompt.

Any ideas on how to search google from cmd prompt?

5
  • Are you dead set on only using command prompt? If yes, then there are no tools in command prompt to allow you to do this. Best you could do is ping www.google.com, but command prompt can not be used in place of a web browser. Now, if you are open to say maybe PowerShell, then that's a different story.
    – TimWagaman
    Commented Feb 8, 2013 at 17:50
  • Ok. I will look into PowerShell. How might you do this in PowerShell? I am pretty unfamiliar with PowerShell.
    – Pareto
    Commented Feb 8, 2013 at 17:57
  • 1
    @R_USER82 in powershell you can use psgoogle
    – gordy
    Commented Feb 2, 2016 at 19:05
  • Yes, you can. Minimum code after open CMD is: start google.com/search?q=my+name+is+pareto Commented Sep 4, 2020 at 13:56
  • I have to add the path to a browser. This works in cmd window: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" google.com/search?q=my+name+is+pareto . (Win10, Chrome is my default browser).
    – ttulinsky
    Commented Sep 7, 2020 at 17:32

10 Answers 10

10

Type the following on the command-line or in the run command (Win+R) and it will open your default browser to search for SEARCHTERM using Google:

start www.google.com/search?q=SEARCHTERM

Note that you have to replace whitespaces in SEARCHTERM with pluses, e.g.

start www.google.com/search?q=Use+cmd+prompt+to+search+a+word+on+google+or+other+search+engine

Alternatively you could also put this command in a batch file:

@start www.google.com/search?q=%1+%2+%3+%4+%5+%6+%7+%8+%9
1
  • 1
    Great! Don't know why this isn't marked as correct answer. Commented Sep 4, 2020 at 13:57
3

You can write in the ps1 file:

function googleSearch{start www.google.com/search?q=$args}
set-alias g googleSearch

then restart your powershell,

g whatwhatwhat
3

easy.

@echo off
color a
setlocal ENABLEDELAYEDEXPANSION
echo Google Batch
echo Made By GenoSans
start https://discord.gg/WwRtbBe
timeout -t 5 /nobreak
:a
cls
echo                                            ,,
echo   .g8'''bgd                               `7MM
echo .dP'     `M                                 MM
echo dM'       `   ,pW'Wq.   ,pW'Wq.   .P'Ybmmm  MM  .gP'Ya
echo MM           6W'   `Wb 6W'   `Wb :MI  I8    MM ,M'   Yb
echo MM.    `7MMF'8M     M8 8M     M8  WmmmP'    MM 8M''''''
echo `Mb.     MM  YA.   ,A9 YA.   ,A9 8M         MM YM.    ,
echo   `'bmmmdPY   `Ybmd9'   `Ybmd9'   YMMMMMb .JMML.`Mbmmd'
echo                                  6'     dP
echo                                   Ybmmmd'
echo.
set /p s=Search: 
set word=+
set str=%s%
set str=%str: =!word!%
start http://www.google.com/search?q=%str%
goto a
2

I created a Batch file 'g.bat' and added it to my PATH. It looks like this:

start www.google.co.uk/search?q=%1+%2+%3+%4+%5

Supports up to 5 words (of course you can add more). Now I can search from CMD or start by typing "g query"

Edit: Credit to Andreas for the inspiration

1

Create an alias with doskey in a .bat or .cmd file containing those lines:

@echo off
title "Foo Bar"
doskey goo=echo off $T echo Googling: $* $T start "" "www.google.com/search?q=$*" $T echo on

We add "" to launch research in the default browser.
However, if you want to goto another search engine, just change it by your browser. For instance, iexplorer for Internet explorer or firefox if you prefer Mozilla Firefox.

Example to use it :

goo stack overflow      // it works ! 

Weakness: If you search a sentence containing & special character that will stop your research until this character.

Example :

goo stack& overflow     // it does not work ! 

Enjoy ! =)

Note: For Linux users having Cygwin or MSYS2, you can add this function in .bashrc file to do the same routine :

goo ()
{
    echo "Googling: $@";
    start "" "www.google.com/search?q=$*" $@
}

Best regards !

0

I suppose you could use wget from the command line.

wget -U "Firefox/3.0.15" http://www.google.com/search?q=SEARCH+TERMS+HERE -O result.html -q

Or -O- -q to output to stdout. Scraping the results from the html will be a different matter entirely, though.

If you do get wget, might as well get grep as well, or just get all of GnuWin32 as it's quite useful. Then you can do stuff like this:

wget -U "Firefox/3.0.15" "http://www.google.com/search?q=wget+google+search" -O- -q 2>&1 | grep -E -o -e "<cite>[^<]+</cite>" | sed -r -e "s/<[^>]+>//g"

... to get the URL of the first link from a Google search. The sky's the limit. Get creative.

(Output of the example command above: isaksen.biz/blog/?p=470)

If you want to display the first title plus the first URL, it gets a little more complicated.

@echo off
setlocal enabledelayedexpansion
set search=%1 %2 %3 %4 %5 %6 %7 %8 %9
for /l %%a in (1,1,8) do if "!search:~-1!"==" " set search=!search:~0,-1!
set search=%search: =+%
wget -U "Firefox/3.0.15" "http://www.google.com/search?q=%search%" -O search.html -q 2>NUL
for /f "tokens=*" %%I in ('grep -P -o "<h3 class=.*?</h3>" search.html ^| sed -r -e "s/<[^>]+>//g"') do (
    echo %%I
    goto next
)
:next
set /p I="http://"<NUL
for /f "tokens=*" %%I in ('grep -E -o -e "<cite>[^<]+</cite>" search.html ^| sed -r -e "s/<[^>]+>//g"') do (
    echo %%I
    del /q search.html
    goto :EOF
)

Usage: search.bat up to 9 search terms here

Example:

C:\Users\me\Desktop>search command line google
googlecl - Command line tools for the Google Data APIs - Google ...
http://goosh.org/

C:\Users\me\Desktop>
0

The code for searching with any number of words:

@echo off
set "searchQuery="
:loop
if "%~1"=="" goto done
set "searchQuery=%searchQuery%+%1"
shift
goto loop
:done
start www.google.com/search?q=%searchQuery:~1%

I saved the file as "g.bat" and put in on the Path. Then you can just open the Command Prompt and write:

g this is a google search

To get google results in your browser.

0
@echo off
set str=%*
start "https://www.google.com/search?q=%str: =+%"
:: or use explorer.exe
explorer.exe "https://www.google.com/search?q=%str%"

for more see by @BK1 at https://stackoverflow.com/a/64885208

-1

Maybe this?

@echo off
:start
cls
echo.
echo G O O G L E Web Search Version 1.1
echo.
echo Type search terms, and your internet browser will open it.
echo.
set /p Web=
start www.google.com/search?q=%Web%
goto start

Save it as .bat and boom!

0
-2
  • open Notepad file
  • type start www.google.com/
  • save the file with .bat extensionthen
  • open the batch file everytime to open Google search
  • no need to go to cmd prompt every time
1
  • This does not solve user need. Hi wants to search for a word, not only open google. Commented Nov 7, 2014 at 10:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.