BLAZE
BLAZE

Reputation: 117

Initialize google chrome by calling a variable

This is what I had in mind:

$chrome= (Start-Process "chrome.exe" "chrome:\\newtab")
$chrome

Pushing enter at the end of the first line loads chrome instantly. But instead of typing in "Start-Process......." each time, there must be a way to get this simple code assigned to something that is faster to type.

When running the second line of the code it just does nothing.

Any ideas?


By the way, I read this and this questions on this site but I still don't understand how to code this properly. I'm a total newcomer to this.


This is an edit in response to the answer given by @vonPryz

 PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\newtab"}
Get-Process : A positional parameter cannot be found that accepts argument 'Launch-Chrome'.
At line:1 char:1
+ PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\n ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand

PS C:\>Launch-Chrome
PS : Cannot find a process with the name "C:\>Launch-Chrome". Verify the process name and call the cmdlet again.
At line:1 char:1
+ PS C:\>Launch-Chrome
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\>Launch-Chrome:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

Tried it in powershell 2 and 1 but still doesn't work sadly.

Upvotes: 0

Views: 1230

Answers (2)

BLAZE
BLAZE

Reputation: 117

I fixed it.

PS C:\function L-C {Start-Process "chrome.exe" "chrome:\\newtab"}
PS C:\ L-C
# Chrome window opens

Upvotes: 0

vonPryz
vonPryz

Reputation: 24071

There are quite a few ways to achieve this.

The first one is to create a function that launches Chrome. Like so,

PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\newtab"}
PS C:\>Launch-Chrome 
# A new Chrome window appears!

Save the function in your Powershell profile so that it'll be included on each new session. This might be the simplest solution, so try it first.

Another one is to create a script file that contains the function. Load the script either by dot-sourcing it on a session, or in your Powershell profile. If you need complex scripts, it might be worthwhile to maintain the scripts on separate path and just use profile to load those.

Third one is to create a Powershell module that contains the function. This is the most complex alternative out of the three solutions.

Upvotes: 1

Related Questions