0

When I execute .\flyway.cmd from a PowerShell, it behaves as I'd expect - output stays in powershell. However, when I run c:\temp\flyway\flyway.cmd (absolute path), it pops open a cmd window instead. Why?

Additional info: Inspired by the question of "What's in the CMD script?" from andyb, I started playing around with different scripts. I eventually found that I can make an exact copy of the current cmd file and that copy runs as expected. This means there's something about the file attributes that is making it do this. It was originally a "blocked" file which I had to unblock in it's properties (windows often does this with files that were downloaded). But it isn't any longer. I can also make it run consistently by running the whole command with cmd /c, but that still doesn't explain what is different.

6
  • 1
    What version of PS? I couldn't replicate this in PS 5.1 on Win 10. Commented Dec 13, 2017 at 1:00
  • 2
    Couldn't replicate in PS 3.0 on Win 2K8R2. What's in the CMD script? Commented Dec 13, 2017 at 1:04
  • Do you get the same behavior if you run the batch file from a PowerShell that was started without profile? (powershell.exe -NoProfile) Commented Dec 13, 2017 at 1:04
  • @andyb, see modifications - very interesting. Commented Dec 13, 2017 at 15:24
  • How do you expect anyone to help you when still the only tangible information you provided is the filename/path? Unblocking the file would hardly have an impact as it's just removing an alternate data stream from the file. File attributes also shouldn't cause this behavior. Please show some actual information (content, permissions, attributes, ADS, ...). Commented Dec 13, 2017 at 21:11

1 Answer 1

2

I think it's due to how PS interprets ".\" For PS, that means you're going to run something. Putting the absolute path to a file, means you're referencing the file as you would from the Windows Explorer.

If you want to run it using an absolute path, use the call operator "&":

& 'c:\temp\flyway\flyway.cmd'

That should do the trick.

You can read more about it on TechNet or The PowerShell Wiki

I hope this helps you!

Happy scripting!

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

3 Comments

.\ does not have the meaning "run something". It's the beginning of a relative path, nothing else. Are you perhaps confusing .\ with the source operator (.)? Anyway, that still doesn't explain the described behavior.
You're right, but the beginning of a relative path is "required" to run a script in PowerShell, it is designed that way. That's why I said, it had that "meaning". You won't be able to run a script only by using a absolute path. That's the real meaning behind my comment. Thanks for the clarification anyway.
but the beginning of a relative path is "required" to run a script in PowerShell That is still incorrect. You can run a script (any executable file actually) without a path as long as its parent directory is listed in the PATH environment variable. Demonstration: 'Write-Host "foo"' > test.ps1; test.ps1; $env:Path += ';.'; test.ps1. A path (regardless of whether it's absolute or relative) is only required if the location of an exectuable file is not in the PATH.

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.