Search code examples
powershellpowershell-cmdlet

Referencing a file containing square brackets ([]) in its path


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?


Solution

  • 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