I have a directory whose name contains square brackets, making handling it in Powershell a little problematic. I've seen advice that to get the ACL of the directory, you should use the GetAccessControl()
method of DirectoryInfo
instead of using the cmdlet Get-Acl
.
Unfortunately, using GetAccessControl()
is not returning any results for any directory. See my script below.
$filename = "C:\somefolder"
$di = get-item $filename
$di | get-acl # this returns the correct ACL data as expected
$di.GetAccessControl() # this shows an empty ACL (see below output)
What I mean by an empty ACL is this:
Path Owner Access
---- ----- ------
The only way I've found to get an instance of $di with a directory name which contains square brackets is to escape the brackets with double back-ticks
$filename = "C:\some``[folder``]withsquarebrackets"
If I do this and then execute $di = get-item $filename
, $di
shows that it contains details on the specified folder. But in this case, different results are returned by the remaining two lines:
$di | get-acl # does nothing, doesn't even show an empty ACL
$di.GetAccessControl() # shows an empty ACL as seen above
Can anyone explain how I can reliably get the ACL for an enumeration of folders, some of which contain square brackets in their names?
Update 2012-02-23 16:31Z After Andy Arismendi reported it worked for him, I established that GetAccessControl() seems to work fine on Powershell 2. The affected machine runs Powershell 1. Any ideas how to resolve this using Powershell 1?
It seems to work for me although the Path
property is null.
$f = Get-Item 'C:\some `[folder`]'
$f.GetAccessControl()
$f.GetAccessControl().Access