PowerShellの基本操作
PowerShellの基本操作
Section titled “PowerShellの基本操作”PowerShellの基本的な操作方法を詳しく解説します。
PowerShellとは
Section titled “PowerShellとは”PowerShellの特徴
Section titled “PowerShellの特徴”定義: PowerShellは、Microsoftが開発したタスク自動化と構成管理のためのシェルおよびスクリプト言語です。
特徴:
- オブジェクトベースの処理
- .NETとの統合
- クロスプラットフォーム対応(PowerShell Core)
- 強力なパイプライン
基本的なコマンドレット
Section titled “基本的なコマンドレット”Get-Command(コマンドの検索)
Section titled “Get-Command(コマンドの検索)”# すべてのコマンドレットを表示Get-Command
# 特定のパターンで検索Get-Command *Process*
# 動詞で検索Get-Command -Verb Get
# 名詞で検索Get-Command -Noun ProcessGet-Help(ヘルプの表示)
Section titled “Get-Help(ヘルプの表示)”# コマンドレットのヘルプを表示Get-Help Get-Process
# 詳細なヘルプGet-Help Get-Process -Detailed
# 完全なヘルプGet-Help Get-Process -Full
# 例を表示Get-Help Get-Process -Examplesファイル操作
Section titled “ファイル操作”Get-ChildItem(ファイル一覧)
Section titled “Get-ChildItem(ファイル一覧)”# 現在のディレクトリのファイル一覧Get-ChildItem
# すべてのファイルを表示(-Force: 隠しファイルも含む)Get-ChildItem -Force
# 再帰的に表示(-Recurse)Get-ChildItem -Recurse
# フィルタリングGet-ChildItem *.txt
# 詳細情報を表示Get-ChildItem | Format-ListSet-Location(ディレクトリ移動)
Section titled “Set-Location(ディレクトリ移動)”# ディレクトリに移動Set-Location C:\Users
# ホームディレクトリに移動Set-Location ~
# 前のディレクトリに戻るSet-Location -Copy-Item / Move-Item / Remove-Item
Section titled “Copy-Item / Move-Item / Remove-Item”# ファイルをコピーCopy-Item source.txt dest.txt
# ディレクトリを再帰的にコピーCopy-Item -Path source_dir -Destination dest_dir -Recurse
# ファイルを移動Move-Item source.txt dest.txt
# ファイルを削除Remove-Item file.txt
# 確認しながら削除Remove-Item file.txt -Confirmプロセス管理
Section titled “プロセス管理”Get-Process(プロセス一覧)
Section titled “Get-Process(プロセス一覧)”# すべてのプロセスを表示Get-Process
# 特定のプロセスを検索Get-Process -Name nginx
# プロセスの詳細情報Get-Process | Format-List *
# CPU使用率でソートGet-Process | Sort-Object CPU -DescendingStop-Process(プロセス終了)
Section titled “Stop-Process(プロセス終了)”# プロセスを終了(ID指定)Stop-Process -Id 1234
# プロセスを終了(名前指定)Stop-Process -Name nginx
# 強制終了Stop-Process -Id 1234 -Forceテキスト処理
Section titled “テキスト処理”Get-Content(ファイル内容の読み取り)
Section titled “Get-Content(ファイル内容の読み取り)”# ファイル内容を表示Get-Content file.txt
# 最初の10行を表示Get-Content file.txt -TotalCount 10
# 最後の10行を表示Get-Content file.txt -Tail 10
# リアルタイムで監視(-Wait)Get-Content file.txt -WaitSelect-String(テキスト検索)
Section titled “Select-String(テキスト検索)”# テキストを検索Select-String "ERROR" file.txt
# 大文字小文字を区別しない(-CaseSensitive: $false)Select-String "error" file.txt -CaseSensitive:$false
# 行番号を表示(-LineNumber)Select-String "ERROR" file.txt -LineNumber
# 複数のファイルから検索Select-String "ERROR" *.logテキスト置換
Section titled “テキスト置換”# テキストを置換(Get-Content file.txt) -replace 'old', 'new' | Set-Content file.txt
# 正規表現を使用(Get-Content file.txt) -replace '\d+', 'NUMBER' | Set-Content file.txt変数とオブジェクト
Section titled “変数とオブジェクト”# 変数の定義$name = "Alice"$age = 25
# 変数の使用Write-Host $nameWrite-Host $age
# 配列の定義$items = @("item1", "item2", "item3")
# ハッシュテーブル$hash = @{ Name = "Alice" Age = 25}オブジェクトの操作
Section titled “オブジェクトの操作”# オブジェクトのプロパティにアクセス$file = Get-ChildItem file.txtWrite-Host $file.NameWrite-Host $file.LengthWrite-Host $file.LastWriteTime
# オブジェクトをフィルタリングGet-Process | Where-Object {$_.CPU -gt 100}
# オブジェクトを選択Get-Process | Select-Object Name, CPU, Memoryパイプライン
Section titled “パイプライン”パイプラインの使い方
Section titled “パイプラインの使い方”# オブジェクトをパイプで渡すGet-ChildItem | Where-Object {$_.Extension -eq ".txt"} | Select-Object Name
# 複数のコマンドレットを連結Get-Process | Where-Object {$_.CPU -gt 100} | Sort-Object CPU -Descending | Select-Object -First 10PowerShellの基本操作のポイント:
- Get-Command: コマンドの検索
- Get-Help: ヘルプの表示
- ファイル操作: Get-ChildItem、Set-Location、Copy-Item、Move-Item、Remove-Item
- プロセス管理: Get-Process、Stop-Process
- テキスト処理: Get-Content、Select-String、テキスト置換
- 変数とオブジェクト: 変数の操作、オブジェクトのプロパティアクセス
- パイプライン: オブジェクトベースのパイプ処理
適切にPowerShellを使用することで、効率的な作業ができます。