tags:

views:

266

answers:

1

Suppose I have a file named "test[1].txt". Both of these commands produce no output:

PS C:\Temp> dir test[1].txt
PS C:\Temp> get-acl test[1].txt
PS C:\Temp>


The good news is that the dir command has a -LiteralPath switch that tells it not to interpret any characters as wildcards:

PS C:\Temp> dir -LiteralPath test[1].txt


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/25/2010   9:57 AM          5 test[1].txt


The bad news is that get-acl has no such switch. This appears to be a known issue.

The only workaround I could come up with, following an observation in that report, is to use UNC paths in this somewhat awkward way:

PS C:\Temp> get-acl \\mymachine\C$\temp\test[[]1[]].txt


    Directory: Microsoft.PowerShell.Core\FileSystem::\\mymachine\C$\temp


Path                                    Owner
----                                    -----
test[1].txt                             MYDOMAIN\myname

I'm using get-acl in a pipeline fed by dir -rec -name. Because I don't know where the wildcards will appear, I'll have to write code to escape them (and convert the paths to UNC format). It's a bit kludgy. Is there a simpler way? I'm using PowerShell v1.0, by the way.

Opinion: Perhaps this is an unavoidable consequence of trade-offs made in the design of PowerShell, but it's unfortunate that every command needs an option to ignore wildcards, rather than having that functionality in the shell itself so that all commands automatically benefit.

A: 

This issue has bit me from time to time and it sucks when you hit a cmdlet without -LiteralPath support. In this case, another work-around is to use the short name of the file. The PowerShell Community Extensions supports this with the Get-ShortPath cmdlet e.g.:

PS> Get-ShortPath '.\foo`[bar`].txt' | Get-Acl -Path {$_}


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\User
    s\Keith


Path                 Owner               Access
----                 -----               ------
FOO_BA~1.TXT         Keith-Laptop-PC\... NT AUTHORITY\SYS...
Keith Hill