DevOps | Scripts | Automation

PowershellTroubleShooting

How to resolve: The specified wildcard character pattern is not valid?

In PowerShell 5.1

In PowerShell 5.1 or below, when wildcard characters are in your file/folder path, retrieving ACL (Get-ACL command) for the object is tricky. For example, my folder name is “test ” f `[” and it is on that shared path “\adserver\SharedFolder1\test ” f `[\“. Let’s try to retrieve ACL for it.

Get-Acl '\\adserver\SharedFolder1\test '' f `[\'

The output you might be seeing is something like this,

Get-Acl : The specified wildcard character pattern is not valid: test ‘ f [
At line:1 char:1
Get-Acl ‘\adserver\SharedFolder1\test ” f `[\’
CategoryInfo : NotSpecified: (:) [Get-Acl], WildcardPatternException
FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.GetAclCommand

To resolve this error, we can use the GetAccessControl() method of the Get-Item command.

(Get-Item '\\adserver\SharedFolder1\test '' f `[\').GetAccessControl()

Output:

Output of ACL

If you are working on child folders then you can use the same GetAccessControl() method.

For PowerShell Core versions (6.0 +)

In PowerShell core version (tested in version 7.0 +) version, GetAccessControl() method is not available. To resolve the above error, we need to use Get-ACL with the LiteralPath parameter.

 Get-Acl -LiteralPath 'C:\Temp\test ''[ folder\'

Output:

Get-ACL with LiteralPath

Please note, -LiteralPath with special characters (‘[) doesn’t work with PowerShell 5.

3 thoughts on “How to resolve: The specified wildcard character pattern is not valid?

  • that’s really helpful point. however, it applies only on v 5.1. do you know any equivalent for v 7.2 and higher?

    • Chirag Nagrekar

      Yes. PS core doesn’t support GetAccessControl() method. I will get back to you on the resolution on PS 7.2 or higher.

    • Chirag Nagrekar

      Check now. The article is updated with PS core version support.

      Thanks for highlighting it.

Comments are closed.