PowerShellは、ファイル操作を安全かつ柔軟に自動化できる強力なツールです。
コピー・移動・削除といった基本操作も、オプション次第で挙動が大きく変わります。
本記事では、PowerShellでよく使うファイル操作コマンドを用途別に整理し、
現場でそのまま使える実例付きで解説します。
PowerShellのファイル操作で使う基本コマンド一覧
| 操作 | コマンド |
|---|---|
| コピー | Copy-Item |
| 移動 | Move-Item |
| 削除 | Remove-Item |
| 新規作成 | New-Item |
| 存在確認 | Test-Path |
| 一覧取得 | Get-ChildItem |
| 内容取得 | Get-Content |
| 内容書き込み | Set-Content / Add-Content |
ファイルをコピーする:Copy-Item
基本構文
|
1 2 |
Copy-Item コピー元 コピー先<code class="whitespace-pre! language-powershell"> |
ファイルをコピー
|
1 2 |
Copy-Item C:\work\data.txt C:\backup\data.txt<code class="whitespace-pre! language-powershell"> |
フォルダを丸ごとコピー
|
1 2 |
Copy-Item C:\work C:\backup\work -Recurse<code class="whitespace-pre! language-powershell"> |
既存ファイルを上書き
|
1 2 |
Copy-Item C:\work\data.txt C:\backup\data.txt -Force<code class="whitespace-pre! language-powershell"> |
よく使うオプション
| オプション | 内容 |
|---|---|
| -Recurse | サブフォルダ含めてコピー |
| -Force | 読み取り専用も含めて上書き |
| -WhatIf | 実行せず動作確認 |
ファイルを移動する:Move-Item
ファイルを移動
|
1 2 |
Move-Item C:\temp\data.txt C:\archive\<code class="whitespace-pre! language-powershell"> |
ファイル名を変更(リネーム)
|
1 2 |
Move-Item old.txt new.txt<code class="whitespace-pre! language-powershell"> |
フォルダを移動
|
1 2 |
Move-Item C:\temp\logs C:\archive\<code class="whitespace-pre! language-powershell"> |
※ 移動先に同名ファイルがある場合はエラーになるため、-Force の併用が有効です。
ファイル・フォルダを削除する:Remove-Item
ファイル削除
|
1 2 |
Remove-Item C:\temp\data.txt<code class="whitespace-pre! language-powershell"> |
フォルダ削除(中身ごと)
|
1 2 |
Remove-Item C:\temp\logs -Recurse<code class="whitespace-pre! language-powershell"> |
強制削除
|
1 2 |
Remove-Item C:\temp\data.txt -Force<code class="whitespace-pre! language-powershell"> |
実行前の安全確認
|
1 2 |
Remove-Item C:\temp\* -WhatIf<code class="whitespace-pre! language-powershell"> |
ファイル・フォルダを作成する:New-Item
ファイル作成
|
1 2 |
New-Item C:\work\sample.txt -ItemType File<code class="whitespace-pre! language-powershell"> |
フォルダ作成
|
1 2 |
New-Item C:\work\logs -ItemType Directory<code class="whitespace-pre! language-powershell"> |
ファイル・フォルダの存在を確認する:Test-Path
|
1 2 |
Test-Path C:\work\data.txt<code class="whitespace-pre! language-powershell"> |
結果:
-
True→ 存在する -
False→ 存在しない
存在チェックしてから処理する例
|
1 2 3 4 |
if (Test-Path C:\work\data.txt) { Copy-Item C:\work\data.txt C:\backup\ }<code class="whitespace-pre! language-powershell"> |
ファイル一覧を取得する:Get-ChildItem
フォルダ内一覧
|
1 2 |
Get-ChildItem C:\work<code class="whitespace-pre! language-powershell"> |
特定拡張子のみ取得
|
1 2 |
Get-ChildItem C:\work -Filter *.log<code class="whitespace-pre! language-powershell"> |
サブフォルダ含めて取得
|
1 2 |
Get-ChildItem C:\work -Recurse<code class="whitespace-pre! language-powershell"> |
ファイルの内容を扱う
内容を読み取る:Get-Content
|
1 2 |
Get-Content C:\work\data.txt<code class="whitespace-pre! language-powershell"> |
内容を書き換える:Set-Content
|
1 2 |
Set-Content C:\work\data.txt "Hello PowerShell"<code class="whitespace-pre! language-powershell"> |
追記する:Add-Content
|
1 2 |
Add-Content C:\work\data.txt "追加行"<code class="whitespace-pre! language-powershell"> |
実務でよくある組み合わせ例
ログを日付フォルダへ移動
|
1 2 3 4 |
$today = Get-Date -Format yyyyMMdd New-Item "C:\archive\$today" -ItemType Directory -Force Move-Item C:\logs\*.log "C:\archive\$today\"<code class="whitespace-pre! language-powershell"> |
バックアップ作成(上書き防止)
|
1 2 3 4 |
if (-not (Test-Path C:\backup\data.txt)) { Copy-Item C:\work\data.txt C:\backup\ }<code class="whitespace-pre! language-powershell"> |
まとめ
PowerShellのファイル操作は、
-
Copy-Item / Move-Item / Remove-Item を押さえる
-
-Recurse / -Force / -WhatIf を正しく使う
-
Test-Path で安全性を確保する
この3点を意識するだけで、
バッチ処理・自動化の安定性が大きく向上します。
日常運用から定期ジョブまで、ぜひ活用してみてください。
