2

I don't want to use bash.

I've looked for any parameter in the FileInfo object, but there isn't something that tells you if the file is executable.

How do I do this?

1 Answer 1

5

Use the standard test utility (/usr/bin/test on Ubuntu):

$isExecutable = $(test -x '/path/to/some/file'; 0 -eq $LASTEXITCODE)

Note: One would expect that using Get-Command with a file path would indicate whether the target file is executable, but that is not reliable as of PowerShell 7.0: any existing file - whether executable or not - is currently reported as a command (of type Application) - see GitHub issue #12625

# !! SHOULD work, but does NOT as of PowerShell 7.0:
# !! any existing file is reported as executable.
$isExecutable = [bool] (Get-Command -ErrorAction Ignore '/path/to/some/file')

Note: Should this ever be fixed, note that you'll always need to specify a path rather than a mere file name, even for files in the current location, e.g., Get-Command ./foo rather than Get-Command foo, because the latter would only look for an foo executable in the directories listed in $env:PATH.

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

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.