익명 16:00

Is there a chmod +x equivalent in Windows 10, 11 to allow running scripts in Pow...

Is there a chmod +x equivalent in Windows 10, 11 to allow running scripts in Powershell/cmd with a specified shebang?

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)?



Top Answer/Comment:

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

상단 광고의 [X] 버튼을 누르면 내용이 보입니다