1

I would like to write cross platform scripts in some interpreted language (Python, Go, Node, whatever).

This is trivial in *nix systems (MacOS, Linux, BSD) as I just need to mark a file as executable using chmod +x ./myfile and the system will execute that script using the specified interpreter in the shebang (#!/usr/bin/env python). I can then run the file as though it were an executable using ./myfile.

I would like to replicate this in Windows, however in Powershell running .\myfile attempts to open that file using the graphical OS "choose what application to run this with" dialog.

Is there an equivalent to chmod +x ./myfile && ./myfile in Windows or do I have to instruct people to use the full command (eg python ./myfile)?

4
  • 1
    No, windows uses file extensions to execute file types. It will not look into the top line of a file to see what it should be executed in. In addition, the chmod +x has nothing to do with the interpretation of the shebang. NTFS certainly supports the +x concept through ACLs (access control lists) and always has. On the flip-side, you could use WSL and write all of your scripts however you want (including bash). A distro running under WSL IS Linux and can execute files from the windows side natively. Also WSL is bad@$$. May 21 at 1:28
  • Here's an example: powershell.exe -ExecutionPolicy Bypass -File "C:\PowerShell\PScript.ps1". So you call Powershell.exe with necessary parameters and -file <script> will the be the full path to the ps1 file. Windows does not execute .ps1 file by design for security. So try the above example or powershell.exe -ExecutionPolicy Bypass -File "C:\PowerShell\PScript.ps1" "scriptarg1". You can run powershell.exe /? to get more detail. May 21 at 2:50
  • Here's an example answer I wrote, I'm sure I have tons of others as well as tons of others from others out there all over the place too superuser.com/questions/1656565/… May 21 at 2:53
  • There is no need to make the files executable before running. If python is installed, then all your users need is the correct file extension to execute it from cmd or powershell like .\myfile.py
    – Cpt.Whale
    May 23 at 17:47

1 Answer 1

0

The closest I know about is Get-ACL/Set-ACL

The equivalent NTFS permissions you need are "Traverse Folder / Execute File"

Example Code:

    $NewAcl = Get-Acl -Path "C:\Pets\Dog.txt"
    # Set properties
    $identity = "BUILTIN\Administrators"
    $fileSystemRights = "Traverse"
    $type = "Allow"
    # Create new rule
    $fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, $type
    $fileSystemAccessRule = New-Object -TypeName 
    System.Security.AccessControl.FileSystemAccessRule -ArgumentList 
    $fileSystemAccessRuleArgumentList
    # Apply new rule
    $NewAcl.SetAccessRule($fileSystemAccessRule)
    Set-Acl -Path "C:\Pets\Dog.txt" -AclObject $NewAcl

Set-ACL Documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-acl?view=powershell-7.2

NTFS Permissions Documentation: https://www.dell.com/support/kbdoc/en-us/000137238/understanding-file-and-folder-permissions-in-windows

NTFS File System Rights Explanation: https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.filesystemrights?view=net-6.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.