0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellの覚書

Last updated at Posted at 2025-08-14

PowerShellの覚書

変数と表示

test.ps1
$a = 123
Write-Output "a = $a"

配列

PowerShellの配列は要素数固定である。

test.ps1
$a = 1, 2, 3 # 配列初期化
Write-Output "a[1] = $($a[1])" # a[1] = 2 を表示

$a += 4 # 要素追加。末尾に要素を追加した配列が新たに生成される

リスト

追加削除可能なリストはSystem.Collections.ArrayListを使う

test.ps1
$myArray = New-Object System.Collections.ArrayList
[Void] $myArray.Add(1) # 末尾への要素の追加 [Void]を書かないと結果が表示されてしまう
[Void] $myArray.Add(2)
[Void] $myArray.Add(3)
$myArray # 1,2,3 が表示される(改行区切り)

ハッシュテーブル

test.ps1
$hash = @{}  # 空のハッシュテーブルを構築
$hash["param"] = 3 # キーと値を追加(すでに値が設定されている場合は上書きされる)
$hash.Add("param", 3)  # キーと値を追加(すでに値が設定されている場合は例外となる)

キーの存在確認

test.ps1
$hash.ContainsKey("param")  

要素についての繰り返し

test.ps1
foreach ($key in $hash.Keys) {
    Write-Host "Key: $key, Value: $($hash[$key])"
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?