Entries from 2017-03-01 to 1 month
Context: You can expand value of variables when you write variable name in double-quoted strings like: > $var = "123" > Write-Host "$var" 123 Problem: Some characters are treated as a part of a variable name, so you can’t use these charact…
Context: You can expand variables in a string like: > $var = "123" > Write-Host "$var" 123 > Problem: You can’t expand array with index: > $arr = @("456", "789") > Write-Host "$arr[0]" 456 789[0] It is also ineffective to add braces: > Wri…
Context: On GitHub, you can see which branches have you created (see “Your branches” section of “Branches” page). Problem: In GitHub API, there is no way to get the creator of the branches. The return values of GitHub References API don’t …
Context: In Windows environment, you can use -Version option to get Powershell pretend to be an older version. Problem: In Linux environment, -Version option is unsupported. Reason: manpage of powershell(1) doesn’t have any content about -…
Context: You use Powershell version 4 or earlier. You have a file that the size is > 10GB. Problem: Size field is folded when you Get-ChildItem to the file that the size is > 10GB. Mode LastWriteTime Length Name ---- ------------- ------ -…
Some methods (e.g. System.IO.File.WriteAllLines()) needs absolute paths for parameters, so you have to convert from relative paths to absolute paths. In PowerShell, there is more than one way to do it ($RelativePath contains relative path …
Context: Return value of Invoke-RestMethod looks like a list of objects: > $headers = @{ Accept = "application/json"; Authorization = "Bearer 0123456789abcdef0123456789abcdef01234567"; } > Invoke-RestMethod -Headers $headers -Method Get -U…
Context: You have a class that has one or more constructors other than default constructor, so you have to define the default constructor explicitly. You use Lombok to generate some methods/fields. You use static code analyzers that warn e…
Context: You can call API of web services (e.g. GitHub) with Invoke-RestMethod. GitHub pagination API contains the last page number in HTTP responce headers. Problem: You can’t access HTTP responce headers with Invoke-RestMethod or the ret…
According to the spec of JSON, an empty string is not a valid JSON. If you pass an empty string to ConvertFrom-Json, it returns $null (doesn’t raise an exception). If you pass an empty JSON (“[]”) to ConvertFrom-Json, it returns empty list…
Context: You can set a value to variable with Set-Variable. > "foo" | Set-Variable var > $var foo You can also overwrite a value of variable like this: > "foo" | Set-Variable var > $null | Set-Variable var > $var Problem: The value of vari…
Context: You want to receive an JSON and check whether the JSON is empty or not. Problem: You cannot check empty JSON with -eq operator, and -eq doesn’t return true/false. > $hoge = (Get-Content ./empty.json | ConvertFrom-Json) > $hoge -eq…
Context: You want to remove a file with Remove-Item. You want to ignore errors even if the file doesn’t exist. Problem: You get an error when execute Remove-Item -Force in the file that does not exist. Reason: The behavior is by design. rm…
Context: You have to write "`r`n" to write CRLF to a file. You can make here document (multi-line string literals) with @‘~’@. Problem: Some articles in web use "`r`n" to split here document in PowerShell like: @' abcde fghij '@.Split("`r`…