LoginSignup
1
1

More than 3 years have passed since last update.

Windows PowerShellで変数に値を格納する

Posted at

目的

  • PowerShellで変数に何かしらの値を格納して出力する方法を書く。

書き方の例

  • PowerShellでは変数定義と変数への値格納を一緒に行うことができる。
  • 下記に変数に任意の文字列を格納する方法と出力する方法を記載する。
  • #の後ろに記載されている文字はコメントである。

# 文字列を格納するとき
$変数名 = "格納したい文字列"

# 数値を格納するとき
$変数名 = '格納したい数値'

# 変数に格納された値をターミナル上に出力するとき
Write-Output $変数名

より具体的な例

  • 下記の処理をPowerShellのターミナルを開き記載することにより、処理を確認することができる。
  • 下記に変数stringに「おはようございます」を格納し、変数numberに「123456789」を格納し、両変数を画面に出力する処理を記載する。

# 変数stringに文字列を格納
$string = "おはようございます"

# 変数numberに数値を格納
$number = '123456789'

# 変数stringを出力
Write-Output $string
->おはようございます

# 変数numberを出力
Write-Output $number
->123456789

まとめ

  • PowerShellで変数に値を格納するときは$変数名 = 値
  • PowerShellで変数を出力するときはWrite-Output $変数名
1
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
1
1