potisanのプログラミングメモ

趣味のプログラマーがプログラミング関係で気になったことや調べたことをいつでも忘れられるようにメモするブログです。はてなブログ無料版なので記事の上の方はたぶん広告です。記事中にも広告挿入されるみたいです。

PowerShell 7 自動変数一覧

PowerShell 7の自動変数一覧です。各自動変数の詳細はMicrosoftの公式ドキュメントを参照ください。

ドキュメント順(アルファベット順)

$$$?$^$_$args$ConsoleFileName$Error$Event$EventArgs$EventSubscriber$ExecutionContent$false$foreach$HOME$Host$input$IsCoreCLR$IsLinux$IsMacOS$IsWindows$LastExitCode$Matches$MyInvocation$NestedPromptLevel$null$PID$PROFILE$PSBoundParameters$PSCmdlet$PSCommandPath$PSCulture$PSDebugContent$PSHOME$PSItem$PSScriptRoot$PSSenderInfo$PSUICulture$PSVersionTable$PWD$Sender$ShellId$StackTrace$switch$this$true

概要(名前、型、説明)

名前 説明
$$ セッションの受信した最終行の最後のトークン。
$? 最後のコマンドが成功した場合はTrue。失敗した場合はFalse。
$^ セッションの受信した最終行の最初のトークン。
$_ $PSItemと同じ。パイプラインオブジェクトの現在のオブジェクト。
$args スクリプトブロック、スクリプト、関数に渡された未宣言引数の値の配列。
$ConsoleFileName セッションで直近に使用されたコンソールファイル(.psc1)のパス。
$Error 直近のエラー情報を持つエラーオブジェクト。
$Event 処理中のイベントを表すPSEventArgsオブジェクト。
$EventArgs 処理中のイベントの引数。
$EventSubscriber 処理中のイベントのイベントサブスクライバー
$ExecutionContent PowerShellホストの実行コンテクストを表すEngineIntrinsicsオブジェクト。
$false False。
$foreach ForEachループの列挙子(enumerator)。
$HOME ユーザーのホームディレクトリのフルパス。
$Host 現在のPowerShellホストアプリケーションオブジェクト。
$input 関数に渡されたすべての入力を列挙する列挙子(enumerator)。
$IsCoreCLR セッションが.NET Core Runtime (CoreCLR)で実行されていれば$true。
$IsLinux セッションがLinuxで実行されていれば$true。
$IsMacOS セッションがMacOSで実行されていれば$true。
$IsWindows セッションがWindowsで実行されていれば$true。
$LastExitCode 最後に実行したWindowsベースドプログラムの終了コード。
$Matches スカラーに対する-match、-notmatch演算子の実行結果。
$MyInvocation 現在のコマンドに関する情報。
$NestedPromptLevel 現在のプロンプトレベル。
$null null。
$PID 現在のセッションのプロセスID。
$PROFILE 現在のユーザーとホストアプリケーションのPowerShellプロファイルのフルパス。
$PSBoundParameters スクリプトまたは関数に渡された引数の辞書。
$PSCmdlet 実行中のコマンドレットまたはアドバンスド関数。
$PSCommandPath 実行中のスクリプトのフルパスとファイル名。
$PSCulture 現在のセッションのカルチャー。
$PSDebugContent デバッグ環境に関する情報。
$PSHOME PowerShellのインストールディレクトリのフルパス。
$PSItem $_と同じ。パイプラインオブジェクトの現在のオブジェクト。
$PSScriptRoot 実行中のスクリプトを含むディレクトリ。
$PSSenderInfo PSSessionを開始したユーザーに関する情報。
$PSUICulture OSのUIカルチャ。
$PSVersionTable セッションのPowerShellバージョン情報。
$PWD 現在のディレクトリのフルパスを表すパスオブジェクト。
$Sender イベントを作成したオブジェクト。
$ShellId 現在のシェルのID。
$StackTrace 直近のエラーのスタックトレース
$switch Switchステートメントが使用するイテレーター。
$this スクリプトプロパティ・メソッドを定義するスクリプトブロックでは拡張しているオブジェクトの参照。
カスタムクラスではクラスに定義されたプロパティやメソッドへアクセスできるクラスオブジェクト。
$true True。

付録

実行環境の確認(Windows環境)

"CoreCLR: {0}" -f $IsCoreCLR
"Linux: {0}" -f $IsLinux
"MacOS: {0}" -f $IsMacOS
"Windows: {0}" -f $IsWindows
<#
CoreCLR: True
Linux: False
MacOS: False
Windows: True
#>

真偽

$true
$false
$null
#True
#False
#

$true = $false
#WriteError: U:\Data\Desktop\ps1 test\test3.ps1:8:1
#Line |
#   8 |  $true = $false
#     |  ~~~~~~~~~~~~~~
#     | Cannot overwrite variable true because it is read-only or constant.

実行環境

$ConsoleFileName # ""
$HOME            # "<User Home Directory Path>"
$Host
<#
Name             : Visual Studio Code Host
Version          : 2020.4.3
InstanceId       : 9e1a194a-de3f-40ed-bf34-40858d7063ea
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : ja-JP
CurrentUICulture : ja-JP
PrivateData      : Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorServicesPSHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
#>
$PSCommandPath # ""
$PSHOME # "C:\Program Files\PowerShell\7"
$PSScriptRoot # ""
$PID # 10172
$PROFILE # "<...>\PowerShell\Microsoft.VSCode_profile.ps1"
$PWD # Path=<...>
$PSVersionTable
<#
Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
#>
$PSCulture # ja-JP
$PSUICulture # ja-JP
$ShellId # Microsoft.PowerShell

$MyInvocation

$MyInvocation
<#
MyCommand             : $MyInvocation
BoundParameters       : {}
UnboundArguments      : {}
ScriptLineNumber      : 0
OffsetInLine          : 0
HistoryId             : 21
ScriptName            : 
Line                  : 
PositionMessage       :
PSScriptRoot          : 
PSCommandPath         : 
InvocationName        :
PipelineLength        : 2
PipelinePosition      : 1
ExpectingInput        : False
CommandOrigin         : Runspace
DisplayScriptPosition :
#>

$switch

$switch.Currentで現在のcase条件を取得できます。

$i = 5
switch (2, 3, 5) {
    0 {$switch.Current}
    1 {$switch.Current}
    $i {$switch.Current}
}
# 2
# 3
# 5

$switch.Reset()を繰り返すと無限ループになります。

CSVデータ

Name,URL
$$,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#section
$?,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#section-1
$^,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#section-2
$_,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#_
$args,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#args
$ConsoleFileName,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#consolefilename
$Error,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#error
$Event,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#event
$EventArgs,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#eventargs
$EventSubscriber,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#eventsubscriber
$ExecutionContent,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#executioncontent
$false,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#false
$foreach,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#foreach
$HOME,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#home
$Host,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#host
$input,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#input
$IsCoreCLR,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#iscoreclr
$IsLinux,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#islinux
$IsMacOS,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#ismacos
$IsWindows,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#iswindows
$LastExitCode,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#lastexitcode
$Matches,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#matches
$MyInvocation,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#myinvocation
$NestedPromptLevel,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#nestedpromptlevel
$null,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#null
$PID,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#pid
$PROFILE,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#profile
$PSBoundParameters,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#psboundparameters
$PSCmdlet,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#pscmdlet
$PSCommandPath,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#pscommandpath
$PSCulture,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#psculture
$PSDebugContent,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#psdebugcontent
$PSHOME,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#pshome
$PStem,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#pstem
$PSScriptRoot,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#psscriptroot
$PSSenderInfo,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#pssenderinfo
$PSUICulture,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#psuiculture
$PSVersionTable,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#psversiontable
$PWD,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#pwd
$Sender,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#sender
$ShellId,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#shellid
$StackTrace,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#stacktrace
$switch,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#switch
$this,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#this
$true,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7#true