Cannot run .py files in PowerShell

Can someone assist me with the possible reasons why I cannot run .py files in PowerShell. I can run them in CMD fine. I thought PowerShell carried the same basic functionality as the CMD has? Below is the error I am getting? Thank you.

PS C:\Users\ajoh\Desktop\Python\py4e\ex_03> : 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:1 char:1

  • CategoryInfo : ObjectNotFound: ( [], CommandNotFoundException
  • FullyQualifiedErrorId : CommandNotFoundException

Suggestion [3,General]: The command 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: “.\”. See “get-help about_Command_Precedence” for more details.
PS C:\Users\ajoh\Desktop\Python\py4e\ex_03>


Try Invoke-Item either with an absolute or a relative path to your *.py file. Read the help (completely including examples) to learn more about.


PS C:\Users\ajoh\Desktop\Python\py4e\ex_03>python

PowerShell doesn’t run commands / invokables from the current directory without you explicitly telling it you want to use the one from the current directory, by prefixing it with . - as the suggestion message states.

Always read the help / error messages. :slight_smile:

Like it says in the error message:

If you trust this command, instead type: ".\".

Or if you don’t want a new window. For some reason it pops a new window in powershell but not in cmd.


Thank you for all your responses. The invoke-item command seemed to work, except the program display’s briefly and then disappears like it does when you just double-click the .py itself. I am going through pyfe courses and while learning the code I am trying to learn how to package and run the script. How to give it to someone else so they can put it into practice. If anyone has any ideas or articles that would help me down this road I would appreciate it. I did look at the get-help.

If you’re running python I’d just be using cmd and not powershell in most cases. There’s not really a huge reason to mix the two most of the time; PS is in large part a whole other world. You could script it entirely in PowerShell and just have it run natively in the shell rather than calling out to the python executable, but that’s a bit of a jump if the code’s already written.

If you wanted, you could define a simple PowerShell function to invoke Python commands, something like:

function py {
    python $Path

and then just run it with py to shorten the necessary command a bit. New-Alias -Name py -Value python might also do the trick a bit more simply.

Using .\ I got my script to appear with the first user input, but after you enter a value it just disappears. I will try creating the PowerShell py function. I am trying to learn how to write a script and then being able to give it to somebody to use. Thanks a lot for your comments.

Like I said, if you run “python” it will stay in the same window, assuming python is in the path.

This is my path. Is there a problem with it?

PS C:\python\py4e\ex_03> .\

Does this work?

PS C:\python\py4e\ex_03> python

Says I need to import the Python module.