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版

第20章. ファイルとディレクトリ

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

20.0 はじめに

システムを管理する際の最も一般的なタスクの1つは、ファイルとディレクトリを扱うことである。これは、コマンドラインでコンピュータを管理する場合にも、スクリプトを書いて自動的に管理する場合にも当てはまる。

幸いなことに、PowerShellはファイルやディレクトリのスクリプティングを、コマンドラインで作業するのと同じくらい簡単にしてくれる。限られたディスクスペースと格闘し、最もスペースを取っているファイルを発見する必要があるときが、その完璧な例だ。

典型的なプログラマは、システムの特定のディレクトリをスキャンする関数を書くことで、このタスクに取り組むかもしれない。ファイルごとに、そのファイルが気にするほど大きいかどうかをチェックする。もしそうなら、リストに追加する。元のディレクトリ内の各ディレクトリについて、プログラマはこの処理を(処理するディレクトリがなくなるまで)繰り返す。

しかし、「どのプログラミング言語でもC言語は書ける」ということわざがある。あなたがその言語に持ち込む習慣や先入観は、その言語の進歩を受け入れるかどうかに直接影響することが多い。

管理シェルであるPowerShellは、サブディレクトリ内のすべてのファイルを訪問したり、あるディレクトリから別のディレクトリにファイルを移動したりといったタスクを直接サポートしている。プログラマ向けの複雑なスクリプトがワンライナーに変わる:

Get-ChildItem -Recurse | Sort-Object -Descending Length | Select -First 10

お気に入りのプログラマのツールキットに飛び込む前に、PowerShellがその分野で何をサポートしているかを確認しよう。多くの場合、PowerShellはプログラマが使うような技を必要とせずにタスクを処理することができる。

20.1 現在地を決定し、変更する

問題

スクリプトやコマンドから現在の場所を特定したり、別のディレクトリに変更したい。

解決策

現在の場所を取得するには、 コマンドレットを使用する。 コマンドレットは、2つの共通プロパティとしてドライブとパスを提供する:Get-Location Get-Location

$currentLocation = (Get-Location).Path

は (Get-Location).Path) の短縮形なので、$pwd 自動変数を使用する。

現在の場所を変更するには、Set-Location コマンドレットを使う。例えば、ホーム・ディレクトリに変更する:

Set-Location ~

ディスカッション

現在地を取得する

Get-Location コマンドレットは、現在の場所に関する情報を返す。返された情報から、現在のドライブ、プロバイダー、パスにアクセスすることができる。

この現在の場所は、PowerShellコマンドとPowerShellから起動するプログラムに影響する。ただし、これは.NET Frameworkとやり取りする場合には適用されない。ファイルシステムと相互作用する.NETメソッドを呼び出す必要がある場合は、必ず完全修飾パスを指定すること:

[System.IO.File]::ReadAllText("c:\temp\file.txt" ...
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