0

I wrote this function in my powershell profile:

function start {
    if ($args[0] == "blender") {
        Invoke-Item 'C:\Program Files\Blender Foundation\Blender 2.90\blender.exe'
    }
}

It gives me the error below on line 1, at character 1:

Unable to find specified file

The file path is correct, since if I copy and paste the code Invoke-Item ... in my powershell it opens Blender normally. What can the problem be?

Thank you!

1
  • Don’t you mean -eq? Commented Nov 6, 2020 at 6:24

1 Answer 1

2

This happens, as your function name collides with Start-Process.:

get-alias -Name start

CommandType     Name                               Version    Source
-----------     ----                               -------    ------
Alias           start -> Start-Process

Renaming the function is the first step for correction like so,

function start-myApps {
    if ($args[0] == "MyApplication") {
        Invoke-Item 'C:\Program Files\MyApplication\MyApplication.exe'
    }
}

But that isn't enough, as it gives another an error:

Start-MyApps "MyApplication"
= : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:19
+     if ($args[0] == "MyApplication") {
+                   ~
    + CategoryInfo          : ObjectNotFound: (=:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

This one is caused because Powershell's equality comparison operator is -eq, not ==.

The working version has different name and operator like so,

function start-myApps {
    if ($args[0] -eq "MyApplication") {
        Invoke-Item 'C:\Program Files\MyApplication\MyApplication.exe'
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That worked! Thanks a lot for helping, I'm not new to programming but very new to powershell :)
@EnricoCortinovis Another common pitfall is that function parameters in defintion uses commas, but calls use spaces. If you use commas in a call, the parameters are converted into an array. Third common one is that escape character is backtick - but in regexes it's still the backslash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.