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版

第9章. シンプルなファイル

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

9.0 はじめに

システムを管理する場合、当然ながらそのシステム上のファイルを扱うのにかなりの時間を費やすことになる。ファイルの内容を取得したり、パターンを検索したり、ファイル内のテキストを置き換えたりするのだ。

このような単純な演算子であっても、PowerShellのオブジェクト指向は、いくつかのユニークで強力な工夫を加えている。

9.1 ファイルの内容を取得する

問題

ファイルの内容を取得したい。

解決策

Get-Content コマンドレットの引数として、 ファイル名を指定する:

PS > $content = Get-Content c:\temp\file.txt

コマンドレットGet-Content の変数構文を使うには、 ファイル名を${} セクションに置く:

PS > $content = ${c:\temp\file.txt}

.NET FrameworkのSystem.IO.File クラスを使用するには、ReadAllLines() またはReadAllText() メソッドの引数に 、ファイル名を指定する:

PS > $content = Get-Content c:\temp\file.txt -Raw
PS > $contentLines = [System.IO.File]::ReadAllLines("c:\temp\file.txt")

ディスカッション

PowerShellは、ファイルの内容を取得する3つの主要な方法を提供している。1つ目は、Get-Content コマンドレットで、この目的のために設計されたコマンドレットだ。実際には、Get-Content コマンドレットは、コンテンツを持つアイテムの概念をサポートする PowerShell ドライブで動作する。これには、Alias:Function: などが含まれる。2番目と3番目のメソッドは、Get-Content 変数構文とReadAllText() メソッドである。

ファイルに対して作業するとき、Get-Content コマンドレットはファイルの内容を一行ずつ返す。その際、PowerShellはその出力行に関する追加情報を提供する。PowerShell が各出力行にプロパティとして付加するこの情報には、その行が発生したドライブとパスなどが含まれる。

PowerShellに(デフォルトの改行ではなく)選択した文字列に基づいてファイル内容を分割させたい場合は、Get-Content コマンドレットの-Delimiter パラメータで指定できる。

は便利だが、PowerShell がこの余分な情報を使用していないときに添付することは、大きなファイルを演算子するスクリプトの速度を低下させることがある。大きなファイルをより迅速に処理する必要がある場合、Get-Content コマンドレットのReadCount パラメータを使用すると、PowerShell がファイルから一度に読み取る行数を制御できる。ReadCount が 1(デフォルト)の場合、PowerShell は各行を 1 行ずつ返す。ReadCount が 2 の場合、PowerShell は一度に 2 行を返す。ReadCount が 1 未満の場合、PowerShell はファイルからすべての行を一度に返す。 ...

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