# How can win\_acl be used to obtain the status of a file?

**URL:** <https://forum.ansible.com/t/how-can-win-acl-be-used-to-obtain-the-status-of-a-file/37790>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [March 21, 2023, 1:29pm UTC](https://forum.ansible.com/t/how-can-win-acl-be-used-to-obtain-the-status-of-a-file/37790 "2023-03-21T13:29:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bin\_Wang](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Bin\_Wang](https://forum.ansible.com/u/Bin_Wang)\
**Post date:** [March 21, 2023, 1:29pm UTC](https://forum.ansible.com/t/how-can-win-acl-be-used-to-obtain-the-status-of-a-file/37790/1 "2023-03-21T13:29:33Z")

</div>

want to obtain the ACL list of a file path through win\_acl and then delete all users except for the three users: System, Administrators, and Service. However, it seems to be unsuccessful. So, I tried to get the JSON format using PowerShell commands and then import variables to execute.

- name: Get current ACL settings on the D  
ansible.windows.win\_shell: |  
(Get-Acl -Path d:).Access | Where-Object {$_.IdentityReference.Value -notmatch “Administrators|SYSTEM|SERVICE”}| Select-Object FileSystemRights, AccessControlType, IdentityReference | ForEach-Object {_  
_$identityReference = $_.IdentityReference -replace '', ‘’  
$jsonObj = @{  
FileSystemRights = $_.FileSystemRights.ToString()_  
_AccessControlType = $_.AccessControlType.ToString()  
IdentityReference = $identityReference  
}  
$jsonObj | ConvertTo-Json  
}  
register: current\_acl\_settings

- name: debug: var: current\_acl\_settings.stdout\_lines

During the process of setting variables, special characters were escaped, which has troubled me for a long time.

ok: [43.248.136.219] =\> { “acl\_settings”: [“{”, " "AccessControlType": "Allow",“, " "IdentityReference": "CREATOR OWNER",”, " "FileSystemRights": "FullControl"“, “}”, “{”, " "AccessControlType": "Allow",”, " "IdentityReference": "BUILTIN\\Users",“, " "FileSystemRights": "AppendData"”, “}”, “{”, " "AccessControlType": "Allow",“, " "IdentityReference": "BUILTIN\\Users",”, " "FileSystemRights": "CreateFiles"", “}”]

}

---

<div class="post-metadata">

**Author:** ![atul\_mohan](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@atul\_mohan](https://forum.ansible.com/u/atul_mohan)\
**Post date:** [August 22, 2023, 4:57am UTC](https://forum.ansible.com/t/how-can-win-acl-be-used-to-obtain-the-status-of-a-file/37790/2 "2023-08-22T04:57:59Z")

</div>

- name: Get current ACL settings on the D drive  
ansible.windows.win\_shell: |  
$excludedUsers = @(“Administrators”, “SYSTEM”, “SERVICE”)  
$aclSettings = (Get-Acl -Path D:).Access | Where-Object { $_.IdentityReference.Value -notin $excludedUsers } | ForEach-Object {  
$identityReference = $_.IdentityReference.Value -replace ‘\’, ‘’  
$jsonObj = @{  
FileSystemRights = $_.FileSystemRights.ToString()  
AccessControlType = $_.AccessControlType.ToString()  
IdentityReference = $identityReference  
}  
$jsonObj | ConvertTo-Json -Depth 1  
}  
$aclSettings -join “,”  
register: current\_acl\_settings

- name: Debug ACL settings  
debug:  
var: current\_acl\_settings.stdout\_lines
