Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

How to escape special characters in PowerShell

Ways to escape special characters in PowerShell are different between its context, and so complecated...

# Context Escaped char " ' \ ` ``
1 String with " "
2 String with " $
3 String with " '
4 String with " `
5 String with ' "
6 String with ' $
7 String with ' '
8 String with ' `
9 Operand of -like with " *
10 Operand of -like with ' *
11 Operand of -match with " *
12 Operand of -match with ' *
13 Placeholder of -F operator *1 { and }


  • ✓ … You can use it to escape
  • ✘ … You cannot use it to escape
  • - … You don't have to escape

Invoke-RestMethod to GitLab API causes mojibake


In PowerShell, You can call REST API with Invoke-RestMethod like:

Invoke-RestMethod -Headers $headers -Method Get -Timeout 10 -Uri ""


The result of Invoke-RestMethod causes mojibake when the response contains non-ASCII characters like Japanese/Chinese characters.


  • Invoke-RestMethod treats the charset of the HTTP response as ISO-8859-1 rather than UTF-8, when the response doesn't have Content-Type: ... charset=utf-8 in HTTP header.
  • The response of GitLab API doesn't have Content-Type HTTP header.


Use Invoke-WebRequest rather than Invoke-RestMethod. You can decode the content of HTTP response as UTF-8 explicitly.

$res = (Invoke-WebRequest -Headers $headers -Method Get -Timeout 10-Uri "")
$con = [System.Text.Encoding]::Utf8.GetString([System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($res.Content))
ConvertFrom-Json $con

Format operator with string contains "{" and "}"


In PowerShell, you can format a string with -F operator like:

  Get-Content foo.csv | ConvertFrom-Csv -Header Name,Code,Address | `
  ForEach-Object {
    "{0} `n {1} {2}" -F $_.Name,$_.Code,$_.Address


-F operator returns error like below when the formatted string contains When "{" and "}".

  Get-Content foo.csv | ConvertFrom-Csv -Header Name,Code,Address | `
  ForEach-Object {
    '\addaddress{{0}}{様}{{1}}{{2}}' -F $_.Name,$_.Code,$_.Address
Error formatting a string: Input string was not in a correct format..
At /home/xxxx/foobar.ps1:31 char:5
+     '\addaddress{{0}}{様}{{1}}{{2}}' -F $_.Name,$_.Code,$_.Address
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (\addaddress{0}{様}{1}{2}:String) [], RuntimeException
    + FullyQualifiedErrorId : FormatError


You should use "{{" and "}}" to output "{" and "}" with format operator, like:

  ForEach-Object {
    '\addaddress{{ {0} }}{{様}}{{ {1} }}{{ {2} }}' -F $_.Name,$_.Code,$_.Address

How to see an AOP pointcut matches to a method signature


  • You are developing an Java application with Spring Framework.
  • You are trying to write an AOP pointcut to weave some logic into a method invocation, but the weaved logic doesn't seems be running.


Mistakes are sometimes in the method signature, sometimes in the pointcut notation, or sometimes you are just running and watching a wrong method to test the weaved logics. So you have to break down the cause of this problem, but (it seems) there is no formulated way to see whether an AOP pointcut matches to a method signature or not.


There are some tools and procedures you can use:

  • You can use Eclipse debug perspective to see whether an aspect is weaved or not. Set a break point in the target method and run some test. If the aspect method is in the call stack view, it is weaved properly.

There are no properties for ordinary characters in PropList.txt


When run the script in next URL with PropList.txt on, result file did not contain character properties for ordinary characters like 'x', 'y', or 'z'.


PropList.txt does not cover all the characters in the Unicode Standard. The properties for all the characters are in Scripts.txt.

Note there are no mention for Scripts.txt on UNICODE CHARACTER DATABASE page.


Use Scripts.txt rather than PropList.txt.