Skip to Content
PowerShellクックブック 第4版
book

PowerShellクックブック 第4版

by Lee Holmes
March 2025
Intermediate to advanced
1002 pages
14h 55m
Japanese
O'Reilly Media, Inc.
Content preview from PowerShellクックブック 第4版

付録A. PowerShell言語と環境

コマンドと式

PowerShell 入力した行を個々の単位 (トークン)に分割し、各トークンをコマンドまたは式の2つの方法のいずれかで解釈する。 式はロジックやフロー制御文(ifforeachthrow など)をサポートするが、コマンドはサポートしない。

、PowerShellがインタプリタ文を解釈する方法を制御したいと思うことが多いだろうから、表A-1に利用可能なオプションをリストアップしておく。

表 A-1. PowerShellの評価コントロール
in文 説明

優先順位コントロール:()

括弧を使って数式で評価の順番を強制するのと同じように、コマンドや式の評価を強制する。

例えば、こうだ:

PS > 5 * (1 + 2)
15

PS > (dir).Count
227

式のサブパースである:$()

括弧を使って数式で評価の順番を強制するのと同じように、コマンドや式の評価を強制する。

しかし、サブパースはサブプログラムと同じくらい強力であり、サブプログラムに論理文やフロー制御文が含まれている場合にのみ必要となる。

この文は、文字列内の動的情報を展開するためにも使われる。

例えば、こうだ:

PS > "The answer is (2+2)"
The answer is (2+2)

PS > "The answer is $(2+2)"
The answer is 4

PS > $value = 10
PS > $result = $(
   if($value -gt 0) { $true }
   else { $false })
PS > $result
True

リスト評価だ:@()

式を強制的にリストとして評価する。すでにリストであれば、リストのままである。リストでない場合、PowerShellは一時的にリストとして扱う。

例えば、こうだ:

PS > "Hello".Length
5

PS > @("Hello").Length
1

PS > ([PSCustomObject] @{
    Property1 = "Hello"
    Count = 100 }).Count
100

PS > @([PSCustomObject] @{
    Property1 = "Hello"
    Count = 100 }).Count
1

DATA評価である:DATA { }

与えられたスクリプトブロックをPowerShellデータ言語のコンテキストで評価する。データ言語は、PowerShell 言語のデータ中心の機能のみをサポートする。

例えば、こうだ:

PS > DATA { 1 + 1 }
2

PS > DATA { $myVariable = "Test" }
Assignment statements are not
allowed in restricted language
mode or a Data section.

コメント

一行コメントを作成するには、# の文字で行を始める。ブロック(または複数行)コメントを作成するには、<##>.

# This is a regular comment

<# This is a block comment

function MyTest
{
    "This should not be considered a function"
}

$myVariable = 10;

Block comment ends ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成AI時代の新プログラミング実践ガイド Pythonで学ぶGPTとCopilotの活用ベストプラクティス

生成AI時代の新プログラミング実践ガイド Pythonで学ぶGPTとCopilotの活用ベストプラクティス

松本 直樹
デジタルアイデンティティのすべて ―安全かつユーザー中心のアイデンティティシステムを実現するための知識

デジタルアイデンティティのすべて ―安全かつユーザー中心のアイデンティティシステムを実現するための知識

Phillip J. Windley, Drummond Reed, 富士榮 尚寛, 柴田 健久, 花井 杏夏, 宮崎 貴暉, 塚越 雄登, 田島 太朗, 名古屋 謙彦, 村尾 進一, 瀬在 翔太, 松本 優大, 安永 未来, 池谷 亮平
Pythonクイックリファレンス 第4版

Pythonクイックリファレンス 第4版

Alex Martelli, Anna Martelli Ravenscroft, Steve Holden, Paul McGuire, 鈴木 駿, 株式会社クイープ

Publisher Resources

ISBN: 9798341626737