PowerShell
Context: You can set a result of cmdlets to a variable by Set-Variable. Problem: You cannot set a result of cmdlets to a property of object by Set-Variable. PS > $foo = New-Object PSObject | Select-Object Property PS > Get-ChildItem | Set-…
In a function that retrieve parameters from pipeline, if you make a parameter mandatory (Mandatory=$true), you will get error when an empty line is passed from the pipeline. If you receive parameters from the data which may contain empty l…
## Context: A mandatory parameter of a function retrieved from pipeline is defined like this: param( [Parameter(ValueFromPipeline=$true, Mandatory=$true)] [string] $foo ) A mandatory parameter of a function retrieved from argument is defin…
ConvertTo-Csv or Export-Csv always add double quote to all columns. Get-Help Export-Csv -Full shows samples of CSV with no double quotes, but it seems there are no options to suppress double quote. This behavior doesn’t depend on system lo…
Problem: Keys of hash tables are case sensitive in most languages (Java/JavaScript/Perl/Ruby/…). But in PowerShell, key of hash table is case insensitive (if you use string as key), so you will bump into errors in code like: PS > $hash = @…
When you pass chars to Sort-Object, the chars sorted in ASCII order. PS > (0x7E..0x21 | ForEach-Object { [char]$_ } | Sort-Object) -join '' !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ But …
If you want to uniq a list in PowerShell, there is more than one way to do it and these are slightly different. # Cmdlets Sort-Object -Unique Sort-Object | Get-Unique Select-Object -Unique 1 Sort elements Yes Yes No 2 Case sensitive in ded…
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: 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 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`…
@' abcde fghij '@.Split("`r`n") | Set-Variable hoge みたいにヒアドキュメント的なことをするとき、Splitの引数に"`r`n"を指定している記事が時々ある。 これは、Windowsのコンソールで上記の内容を直接タイプしているときは問題ない。 しかし、上記の内…
PowerShellにおける文字コード周りの扱いがカオスすぎたので表にまとめたが、依然としてカオスすぎる。対応しそうな処理の方針がまるでバラバラになっている。 # 処理 デフォルト 変更方法 自動判定可能な文字コード 1 Get-Content(ファイルの読み込み) シ…
PowerShellをインストールした直後は、外部.ps1ファイルの内容を読み込むとエラーが発生する。PowerShell_profile.ps1すら読めない。 対策としては、Set-ExecutionPolicyコマンドレットを実行して、外部ファイルの実行を許可してやればよい。
UNIXでいうところのdiff -r -q before/ after/ができないか調べたが、これに該当するコマンドレットはない模様。Compare-Objectでは、まずディレクトリの比較ができない。 対策は現状不明。Get-ChildItemとForEach-ObjectとGet-ContentとCompare-Objectの組…
テキストの重複排除をするために、UNIXを使ってるときはよくcat | sort | uniq として、コンソールへテキストをコピペするのだけれど、PowerShellではこの最初の「引数を取らないcat」に対応するコマンドレットがないみたい。 対策としては、クリップボード…