Problem with Powershell script using a path with square bracket containing a dash

I have a batch file that calls a script and in the script it has a command similar to this that reports an error.

This is an example of one portion of the script that is giving me trouble.

$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")

When run as a script from a batch command via the powershell -File scriptname.ps1 I get the following error

The specified wildcard pattern is not valid: [Sun-King] Master - Photo Set #1
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RuntimeException

Any help would be appreciated.

You might have to escape special characters in your path, such as the pound/hash # sign

https://ss64.com/ps/syntax-esc.html

There’s some kind of a bug in ps5 with literalpath and wildcards.
You might have to pipe to where-object.

https://social.technet.microsoft.com/Forums/windows/en-US/3a7c057f-babf-4afe-b014-c7ed55b119f9/getchilditem-files-includes-literalpath-and-square-brackets?forum=winserverpowershell

For example, this lists files or folders starting with a or b:

get-item [a-b]*

-include, -exclude, -literalpath, and -filter, all have problems in ps5.

AlexW

The escaping issue is between the square brackets, the pound sign is okay.

if I change the directory and file name

$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")

to

$(Get-Item -LiteralPath 'C:\A\[Sun King] Master - Photo Set #1\[Sun King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")

and then run the ps1 file with the command

pwsh -f filelist.ps1

it works fine but I have far too many directories (including some that are archived) to replace each and every occurrence of the dash between the square brackets.

js

I’m running PowerShell v6.0.4.

Any idea on a workaround to the problem?

Thanks in advance

Actually, it works for me in PS 5.1.16299.611 and 6.1.0-preview.2. I’m getting confused.

Sample directory of images

C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg
C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg
C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg

contents of script named filelist.ps1

$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
$(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")

run at windows command line

pwsh -f filelist.ps1

Results in this error

Tue 08/21/2018 15:05:24.71 C:\A\[Sun-King] Master - Photo Set #1>pwsh -f filelist.ps1
The specified wildcard character pattern is not valid: [Sun-King] Master - Photo Set #1
+ CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RuntimeException

What os are you in?

js

It is being run under Windows 7.

Are you sure of the pwsh version?

pwsh -version

Tue 08/21/2018 15:05:42.95 C:\A[Sun-King] Master - Photo Set #1>pwsh -version
PowerShell v6.0.4

I’m not sure why the -LiteralPath or even using single qoutes is attempting to parse the path as a wildcard, but maybe you can try a different method. It looks like you are just getting JPG’s from a dir, so can see if this works:

$files = Get-ChildItem -Path 'C:\A\[Sun-King] Master - Photo Set #1\*.jpg'

$date = Get-Date "2/16/2017 4:32:02 PM"
foreach ($file in $files) {
    $file.CreationTime=$date
    $file.LastAccessTime=$date
}

If it does not, you will need to escape the characters, like the square brackets. The square brackets are most likely the issue as they are regex meta characters. So, you may need to make the path like so:

'C:\A\`[Sun-King`] Master - Photo Set #1\*.jpg'

Strange. I tried powershell core in win7 sp1 64-bit and had no errors. I guess you can try using ‘?’ instead of brackets and -path instead of -literalpath. You don’t really need either ‘$’.

Rob & js

Tried both ideas and still same issue. I’m at a loss. I’m going to maybe see if I can do this via a cygwin session as a workaround for directories with a dash between square brackets.

And it is the dash that is mucking things up. If I copy the directory and contents and rename said directory and contents with an underscore, space, etc it works flawlessly. Unfortunately renaming every single file/directory currently on disk and the ones archived is not feasible.

How about a ? instead of the dash? Are you sure it’s not an emdash or something?

js

Yes I tried the ? in place of the - and no change (and yes I’m sure it is a dash and not an em dash). Just tried it with an em dash (alt 0151) in place of the - and it worked fine…it is just the dash that breaks it.

 

You can rename like this. Since -newname can get input from the pipeline, it can take a script block:

get-childitem | rename-item -newname  { $_.name -replace '-','_' }

js

Unfortunately, like I said, renaming is not an option as it breaks with the archived files. I’ll just have to keep digging and seeing what I can come up with.

You can try replace instead of rename to escape the rgex .

$(Get-Item -LiteralPath (('C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg' -replace '\]','`]') -replace '\[','`[')).CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")

kvprasoon

Doesn’t help the situation. The dash in the Sun-King is the problem. It is not the square brackets.