Table of Contents
To understand how to get permissions on a folder and subfolder in PowerShell, you must first understand what folder and subfolder permissions are. A folder’s permissions establish who can access and modify files and folders. A subfolder permission is a permission set for a folder containing other folders. In general, permissions set on a folder will propagate to any subfolders unless you specify otherwise.
Permissions can be set for users or groups, such as Administrators
, Users
, and Guests
. The permissions include Full Control
, Modify
, Read & Execute
, List Folder Contents
, Read
, and Write
. To proceed further, knowing the folders and sub-folders in the current directory is crucial. For instance, the current working directory for this article is located at C:\Test.
C:\Test consists of two text files (File1.txt
and File2.txt
) and folders (FolderG
and FolderH
). The items in each folder include the following:
- C:\Test\FolderG – It contains two text files (
File1.txt
andFile6.txt
). - C:\Test\FolderH – It has one text file (
File6.txt
).
So now you know the directories’ details, let’s continue learning to get permission.
Using Get-Acl
Command
Use the Get-Acl
command to get permissions on the specified folder in PowerShell. Get-Acl cmdlet is used to get Access control List(ACL) for files and folders.
1 2 3 |
Get-Acl -Path "C:\Test" |
1 2 3 4 5 |
Path Owner Access ---- ----- ------ Test DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... |
In the above code, the Get-Acl
cmdlet was used to get the Access Control List (ACL) for a file or folder containing the permissions set for that file or folder.
If you notice, you see … here after FullControl as it doesn’t show you complete text.
To get the complete text, use Format-table
cmdlet with -wrap option.
Here is an example:
1 2 3 |
Get-Acl -Path "C:\Test"|Format-table -wrap |
1 2 3 4 5 6 7 8 9 |
Path Owner Access ---- ----- ------ Path Owner Access ---- ----- ------ powershell DESKTOP-MDAC0NJ\user NT AUTHORITY\SYSTEM Allow FullControl BUILTIN\Administrators Allow FullControl DESKTOP-MDAC0NJ\user Allow FullControl |
Get Permissions on current working directory
To get permissions in current working directory, simply use get-acl
cmdlet without any parameters. It will give you Access control list for current working directory.
1 2 3 |
Get-Acl |
1 2 3 4 5 |
Path Owner Access ---- ----- ------ Test DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... |
Using Get-ChildItem
with Get-Acl
Cmdlet
Use Get-Childitem
with the Get-Acl
cmdlet to get permissions on folders and subfolders in PowerShell.
1 2 3 |
Get-ChildItem -Path "C:\Test" -Recurse | Get-Acl |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Directory: C:\Test Path Owner Access ---- ----- ------ FolderG DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... FolderH DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... File1.txt DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... File2.txt DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... Directory: C:\Test\FolderG Path Owner Access ---- ----- ------ File1.txt DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... File6.txt DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... Directory: C:\Test\FolderH Path Owner Access ---- ----- ------ File6.txt DESKTOP-MDAC0NJ\user BUILTIN\Administrators Allow FullControl... |
In the above code, TheGet-ChildItem
cmdlet retrieves a collection of child items (files and directories) in the specified folder. The -Path
parameter specifies the path to the folder to search, which is C:\Test. The -Recurse
parameter indicates that the command should also search recursively through all subfolders.
The |
pipeline operator takes the output from the Get-ChildItem
command and sends it as input to the next command in the pipeline. And theGet-Acl
cmdlet retrieves the Access Control List (ACL) for the input object, each file and directory returned by the Get-ChildItem
command.
This command’s output shows the permissions set for each file and directory, including the names of users and groups that have access and the level of access they have.
Considering the above solutions, understanding folder and subfolder permissions are essential for setting and managing permissions effectively. Using the Get-Acl
and Get-ChildItem
, you can easily get and export permissions for folders and subfolders in PowerShell. With these examples, you can confidently start managing folder and subfolder permissions.
That’s all about how get permission on folders and subfolers in PowerShell.