17

Example: You have a shortcut s to SomeProgram in the current directory.

In cmd.exe, you can type s and it will launch the program.

In PowerShell, typing s gives:

The term 's' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

If you type s.lnk or SomeProgram, it runs the program just fine.

How can I configure PowerShell to execute shortcuts just like programs?

6 Answers 6

21

You can also invoke a shortcut by using the "invoke-item" cmdlet. So for example if you wanted to launch "internet explorer.lnk" you can type the following command:

invoke-item 'Internet Explorer.lnk'

Or you could also use the alias

ii 'internet explorer.lnk'

Another cool thing is that you could do "invoke-item t.txt" and it would automatically open whatever the default handler for *.txt files were, such as notepad.

Note If you want to execute an application, app.exe, in the current directory you have to actually specify the path, relative or absolute, to execute. ".\app.exe" is what you would need to type to execute the application.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried over Remote Power Shell session. The problem is it doesn't launch the UI. For instant, I try launching Visual Studio. I can see its process yet no UI is shown when I connect to the computer using RDP. Any idea?
Royi, it's because it's running in a different scope/session, not the one you've RDP'ed to. Also, the above answer doesn't work for executing exe with or without using parameters, nor does Start-Process. Please advise if I'm missing something and thanks.
9

On my Vista system typing S won't launch a lnk file unless I have the environment variable PATHEXT set with .lnk in the list. When I do. S will work in cmd.exe and I have to do .\S in powershell.

3 Comments

cmd automatically adds the current directory "." to your PATH environment variable. PowerShell considers this to be a security risk, and forces you to type the .\ path explicitly. As in linux, you can add "." to your PATH environment variable to restore the "dangerous" behavior...
I think you also have to type the full file name, s.lnk, as slipsec said
Jaykul/slipsec, the answer I gave is explaining that you can add .lnk to the the PATHEXT environment variable and then you can launch a shortcut from the command line without adding .lnk.
5

After adding ;.LNK to the end of my PATHEXT environment variable, I can now execute shortcuts even without the preceding ./ notation. (Thanks bruceatk!)

I was also inspired by Steven's suggestion to create a little script that automatically aliases all the shortcuts in my PATH (even though I plan to stick with the simpler solution ;).

$env:path.Split( ';' ) | 
  Get-ChildItem -filter *.lnk | 
  select @{ Name='Path'; Expression={ $_.FullName } }, 
         @{ Name='Name'; Expression={ [IO.Path]::GetFileNameWithoutExtension( $_.Name ) } } | 
  where { -not (Get-Alias $_.Name -ea 0) } | 
  foreach { Set-Alias $_.Name $_.Path }

Comments

2

I don't believe you can. You might be better off aliasing commonly used commands in a script that you call from your profile script.

Example -

Set-Alias np c:\windows\notepad.exe

Then you have your short, easily typeable name available from the command line.

Comments

2

For one, the shortcut is not "s" it is "s.lnk". E.g. you are not able to open a text file (say with notepad) by typing "t" when the name is "t.txt" :) Technet says

The PATHEXT environment variable defines the list of file extensions checked by Windows NT when searching for an executable file. The default value of PATHEXT is .COM;.EXE;.BAT;.CMD

You can dot-source as described by others here, or you could also use the invocation character "&". This means that PS treats your string as something to execute rather than just text. This might be more important in a script though.

I'd add that you should pass any parameters OUTSIDE of the quotes (this one bit me before) note that the "-r" is not in the quoted string, only the exe.

& "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe" -r | out-null

Comments

0

You can always use tab completion to type "s[TAB]" and press ENTER and that will execute it.

Comments

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.