Referencing a file containing square brackets ([])

2019-05-09 23:25发布

问题:

I'm quite new to Powershell and I'm using the Get-Hash cmdlet:

This works:

PS M:\> Get-Hash -Path "C:\Users\medmondson\Desktop\New Folder\database.adp" -Algorithm SHA512

This fails:

PS M:\> Get-Hash -Path "C:\Users\medmondson\Desktop\New [Folder]\database.adp" -Algorithm SHA512
Get-Hash : Cannot bind argument to parameter 'Path' because it is an empty array.

I understand this is likely due to the square brackets in the path [] which are valid under windows operating systems. So how do I get powershell to escape these?

回答1:

Try to use ` backtick to escape the square bracket and enclose the whole string in single quote.

PS M:\> Get-Hash -Path 'C:\Users\medmondson\Desktop\New `[Folder`]\database.adp' -Algorithm SHA512