14

This is a very annoying thing, to run any .exe that is inside the current directory in Powershell, I am required to use a .\ ahead.

For example, if I have the program fastboot.exe inside the current folder I can not simply type fastboot, as it does inside cmd. I'm forced to type:

.\fastboot

If I just type fastboot (without .\ I get some error like:

The term 'fastboot' 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.

+ fastboot + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (fastboot:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

How to avoid this?

7
  • Could you be a little more specific? I just checked mine and that's not the case. I'm running PS version 5.1. I typed "cd C:\users\xxx\desktop" and then typed "start xxx.xxx" and it opened without a leading ".\" - Could you provide a snippet of your code? The .\ might be needed for another reason.
    – Andrew
    Commented Nov 6, 2018 at 1:54
  • to run any command...are you sure this is required for all/any commands? Not just PowerShell scripts? Commented Nov 6, 2018 at 1:56
  • I corrected the post, it would not be "command", but ".exe". Commented Nov 6, 2018 at 2:08
  • can you provide the response when you try to use the command like you expect?
    – manbearpig
    Commented Nov 6, 2018 at 2:18
  • 2
    This is a security feature. In this case, Powershell does the unix/linux thing and does not run commends from the current directory. More here: unix.stackexchange.com/questions/4430/…
    – uSlackr
    Commented Nov 12, 2018 at 21:53

2 Answers 2

6

If you don't like typing .\command.ps1, You can just type command and hit tab. If there is a command.ps1 file in the current directory it will be auto-completed.

I think this is ergonomic enough to keep the security benefit of the default behavior.

11

Powershell does not load commands from the current location by default.

Suggestion [3,General]: The command xxx.exe was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type ".\xxx.exe". See "get-help about_Command_Precedence" for more details.

So you are left with two options

  • Lower the security barrier by adding the current directory to you path

     $env:path ="$($env:path);."
    
  • Get into the habbit of always prefixing the command with .\

4
  • 1
    I've never heard that cmd.exe's behavior was insecure. If that's a true security vulnerability in cmd.exe I'm pretty sure Microsoft would have to updated it...
    – Carl Walsh
    Commented Aug 6, 2019 at 3:05
  • 1
    @CarlWalsh - This is specific to Powershell and the reasoning is the same is in this question. Commented Aug 6, 2019 at 5:51
  • 1
    Yes, but if PowerShell thinks it is a security problem to trust the current directory as the PATH, I'm wondering why it's not a security problem for cmd to have that behavior. (Maybe part of this is because PS lets you change directories to network locations or even drives with custom providers? But in cmd you can cd into a network share mapped to a drive, and run untrusted programs there.) Put another way, if you add . to the PATH then PS is just as secure as cmd?
    – Carl Walsh
    Commented Aug 6, 2019 at 6:13
  • 2
    @CarlWalsh - I don't know why cmd behaves differently in that regard but I would assume it is left in for backwards compatibility. Afaik, this is a security problem in cmd. If I can get a domain admin to open a command prompt on my computer, navigate to a folder under my control where I've put a malicious dir executable: I can have it do whatever I want. You should post your question as a new question. Commented Aug 6, 2019 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .