Execute shortcuts like programs

Tags: powershell

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?


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.

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 }

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.

By : bruceatk

This video can help you solving your question :)
By: admin