POWERSHELL   Powershell – Get-Content error if filename contains brackets



I needed to get the first 5 bytes of a file using the powershell:

PS D:\> Get-Content "D:\TEST\file.txt" -Encoding byte -TotalCount 5
102
100
115
97
102

But for some files the Get-Content command returns the following error "A parameter cannot be found that matches parameter name 'Encoding'"

PS D:\Scripting\CORRUPT> Get-Content "D:\TEST\file[1].txt" -Encoding byte -TotalCount 5
Get-Content : A parameter cannot be found that matches parameter name 'Encoding'.
At line:1 char:44
+ Get-Content "D:\TEST\file[1].txt" -Encoding <<<<  byte -TotalCount 5
    + CategoryInfo          : InvalidArgument: (:) [Get-Content], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand

The error is returned when the fullname contains brackets ('[‘ or ‘]').
By adding "-LiteralPath" to the command the problem is solved:

PS D:\Scripting\CORRUPT> Get-Content -LiteralPath "D:\TEST\file[1].txt" -Encoding byte -TotalCount 5
102
100
115
97
102

Leave a Reply

Your email address will not be published. Required fields are marked *