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.
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.
I fixed it.
PS C:\function L-C {Start-Process "chrome.exe" "chrome:\\newtab"}
PS C:\ L-C
# Chrome window opens
Related
i am attempting to create a power shell script that will help with automation (collecting data off a website).
I have several lines of commands saved in a script (.ps1), in windows powershell ISE (i have trimmed a small section below)
$ieObject = New-Object -ComObject 'InternetExplorer.Application'
$ieObject.Visible = $true
$ieObject.Navigate('---')
$currentDocument = $ieObject.Document
$currentDocument.IHTMLDocument3_getElementById("serialno")
My issue, is that when i copy paste each line by line from above into the terminal, it returns the result i want.
However when i run the actual script using the ise, i get various errors such as
You cannot call a method on a null-valued expression.
At C:\Users\--\--\script.ps1:13 char:47
+ $currentDocument.IHTMLDocument3_getElementById <<<< ("serialno")
+ CategoryInfo : InvalidOperation: (IHTMLDocument3_getElementById:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I feel that the powershell script is executing differently to how I need it to, any help would be great. thanks
The instance has not received all data yet. Add
do { Start-Sleep -Seconds 1} until ($ieObject.ReadyState -eq 4)
after instantiating $ieObject, that seems to do the trick.
I recommend against automating Internet Explorer, that's comprehensively last century technology. If you need to parse HTML document from a PowerShell script, using built-in Invoke-WebRequest is the easiest way.
I know, this has been asked a few times but I cannot seem to get it working. Basically, I am using multiple ps1 files so I don't have to have a very large script, in this case, its two files (so far):
Migration-Main.ps1
Migration-Companies.ps1
I want to call Migration-Companies.ps1 from Migration-Main.ps1 using invoke expression but this seems to only work if there is only one parameter in the target script, in my case there are two:
param (
[parameter(mandatory=$false )]
[datetime]$CompanyDateCull,
[parameter(mandatory=$false )]
[string]$CompanyManagerCull
)
Write-Output $CompanyDateCull
So if I try to call the script like so:
Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCull -CompanyManagerCull "Fred""
I will get the following result:
Invoke-Expression : A positional parameter cannot be found that accepts argument 'Fred'.
At C:\Users\user\OneDrive\Scripts\Migrations\Imports\FastTrack-Migration-Main.ps1:282 char:1
+ Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCul ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand
But if i remove the second parameter
Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCull"
it works:
Saturday, 21 September 2013 12:00:00 AM
I have tried running the file with other commands as well, such as simply calling PowerShell.exe:
PowerShell.exe -file $Script_Companies -CompanyDateCull $CompanyDateCull -CompanyManagerCull $CompanyManagerCull
But this stops any of my dot sourced functions from being accessible to the script being called, which is a requirement.
Any help would be much appreciated.
For the first query you need escape the double quotes with backtick(`) :
Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCull -CompanyManagerCull `"Fred`""
This may look a little strange, but I am trying to open powershell within a batch file to set Chrome as the default browser. I know this seems counter intuitive, but I promise for this application and the rest of the content in the batch file, this is the route I have to go.
This is the bit of string I am using
start powershell.exe -noexit -command "$chromePath = "${Env:ProgramFiles(x86)}\Google\Chrome\Application\"
$chromeApp = "chrome.exe"
$chromeCommandArgs = "--make-default-browser" & "$chromePath$chromeApp" $chromeCommandArgs
but once it launches powershell, I get a missing terminator error.
Specifically this
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Can anyone help a novice identify what I'm missing?
This came up in a discussion on another question I asked on running executables in Windows Powershell
Whenever I run 7z in my workplace machine that has PS 2.0, I get this error
Bad numeric constant: 7.
At line;1 char:2
+ 7 <<<< z
+ CategoryInfo : ParserError: <7:String> [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : BadNumericConstant.
Note that this error occurs even if I give the full filename 7z.exe. However, if I go into Command Line mode by entering cmd it works without any problems.
Any help would be appreciated.
Try this:
& "Path\to\7z.exe" arg1 arg2 etc
It sounds like PowerShell is trying to interpret your [String] literally as an [Int]
I've been working on this for 2 hours and I'm pulling my hair out. I was working in ISE on my profile script. I don't know if it's relevant but I was trying to store some commands in a variable and execute them later. Anyway I started getting a weird error when trying to create a here-string:
PS > $foo = #"
Ordinarily I would expect
>>
to follow, but instead I get this message:
PS C:\Users\lamartin> #"
The string is missing the terminator: "#.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
To make a long story short I have discovered that ALL my multi-line commands are broken, but only in ISE:
PS > gci |
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : EmptyPipeElement
PS > gci |% {
Missing closing '}' in statement block.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndCurlyBrace
I removed all profiles and all modules. I verified there was nothing extra being loaded (that I could find). Here is something interesting I did find out. It turns out that Powershell actually ALWAYS errors whenever you do a continuation. It just hides it:
http://connect.microsoft.com/PowerShell/feedback/details/371321/benign-errors-for-missing-closing-are-left-in-the-error-variable
And sure enough if I type in one of the above commands in a regular window, while it works silently, if I later go back and look at the $error object there is an identical error. So basically ISE has decided to stop supressing these errors. I looked at all the settings I could think of $WarningPreference $ErrorActionPreference, etc but they are the same in ISE and normal console.
Thanks in advance for any help.
Update
Wow I feel stupid. Apparently this is expected behavior. I could have SWORN that it was behaving differently just a little bit earlier. I guess I was tired.
In PowerShell ISE, you can run a multiline command in the Command Pane. Press SHIFT+ENTER to enter each line of a multiline command, and press ENTER after the last line to execute the multiline command. You can find this in How to Use the Console Pane in the Windows PowerShell ISE.