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版

第16章. 環境への意識

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

16.0 はじめに

スクリプトの多くは分離して動作するように設計されているが、スクリプトの名前、現在の作業ディレクトリ、環境変数、一般的なシステムパスなど、分離された実行環境に関する情報をスクリプトに与えておくと便利なことが多い。

PowerShellは、コマンドレットや組み込み変数から、.NET Frameworkから提供される機能まで、この情報を取得する方法をいくつか提供している。

16.1 環境変数の表示と修正

問題

システムの環境変数と対話したい。

解決策

環境変数にアクセスするには、通常のPowerShell変数とほぼ同じ方法でアクセスする。唯一の違いは、ドル記号($)と変数名の間にenv:

PS > $env:Username
Lee

この方法で環境変数を変更することもできる。例えば、カレント・ディレクトリを一時的にパスに追加する:

PS > Invoke-DemonstrationScript
Invoke-DemonstrationScript.ps1: The term 'Invoke-DemonstrationScript.ps1' is not
recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.

Suggestion [3,General]: The command Invoke-DemonstrationScript.ps1 was not found,
but does exist in the current location. PowerShell does not load commands from
the current location by default. If you trust this command, instead type:
".\Invoke-DemonstrationScript.ps1". See "get-help about_Command_Precedence"
for more details.

PS > $env:PATH = $env:PATH + ".;"
PS > Invoke-DemonstrationScript
The script ran!

ディスカッション

バッチファイルでは、環境変数が一時的な情報を保存したり、バッチファイル間で情報を転送したりする主な方法である。PowerShell変数やスクリプトパラメータは、これらの問題を解決するより効果的な方法だが、環境変数は、システムのパス、一時ディレクトリ、ドメイン名、ユーザー名など、一般的なシステム設定にアクセスする便利な方法を提供し続けている。

PowerShell は、環境プロバイダを通じて環境変数を表示する。このコンテナは、ファイルシステムやレジストリ・プロバイダのアイテムを扱うのと同じように、環境変数を扱うことができる。デフォルトでは、PowerShellはこの情報へのアクセスを提供する env:ドライブ(c:

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