PowerShellのチートシート


基本

ヘルプを参照する

Get-Help TargetCommand

コンソールに出力を行う

echo 'Hello World'
Write-Host 'Hello World'
Write-Output 'Hello World'

バージョン情報を確認する

$PSVersionTable
Get-Host

コンピューター情報を取得する

Get-ComputerInfo

行単位のコメントアウトとブロックのコメントアウト

# comment
<#
comment1
comment2  
#>

特別な変数

変数の表記 説明
${$} 1つ前のコマンド履歴
${^} 1つ前のコマンド履歴
$_ パイプラインで回している現在のオブジェクト
$args パラメーター
$env:*** 各種環境変数(e.g. $env:COMPUTERNAME)
$Error 直近のエラー情報
$false false
$HOME ユーザーのホームディレクトリ
$Host PowerShellの情報
$MyInvocation スクリプトの実行情報(eg. $MyInvocation.InvocationNameで実行パス)
$null null
$Profile ユーザープロファイルのパス
$Pwd カレントディレクトリのパス
$true true

比較演算子

演算子 説明
-lt より小さい(less than)
-le より小さいか等しい=以下(less than or equal to)
-gt より大きい(greater than)
-ge より大きいか等しい=以上(greater than or equal to)
-eq 等しい(equal to)
-ne 等しくない(not equal too)
-like ワイルドカードを含めて文字が一致する
-match 正規表現で一致する
-contains 要素に含まれる

変数の宣言

$a = 1
$b = 'a'
$c = "b"

ワイルドカードと正規表現

$str = 'example.co.jp'
$str -like 'example*'

$str  -match '[^\.]+'
$matches

Select-String '[^\.]+' -input $str -AllMatches | Foreach {$_.matches}

$ms = ([regex]'[^\.]+').Matches($str)

ディレクトリ、フォルダー、ファイルの表示 cf.Get-ChildItem

カレントディレクトリのファイルとフォルダーを表示

ls
Get-ChildItem

指定したディレクトリのファイルとフォルダーを表示

Get-ChildItem targetdirectory

カレントディレクトリ以下を再帰的に表示(階層の深さはオプション)

Get-ChildItem -Recurse -Depth 2

カレントディレクトリのファイルのみを表示

Get-ChildItem -File

カレントディレクトリのフォルダーのみを表示

Get-ChildItem -Directory

特定の名称にマッチする対象の抽出

Get-ChildItem -Filter *.txt

特定パスにマッチし、拡張子で絞り込み、名称で除外した対象の抽出

Get-ChildItem -Path C:\sample\* -Include *.txt,*.log -Exclude test*

名称のみの抽出

Get-ChildItem -Name

フルパスのみを出力

Get-ChildItem | ForEach-Object{Write-Host $_.FullName}

ファイル・フォルダーの指定した要素だけを抽出する

Get-ChildItem | Select-Object BaseName,DirectoryName,FullName,Name
Get-ChildItem | Select BaseName,DirectoryName,FullName,Name

ディレクトリ・ファイルの操作や移動 cf.New-Item

カレントディレクトリを設定する

cd C:\sample\
Set-Location C:\sample\
sl C:\sample\
chdir C:\sample\

ファイルの中身を表示する

cat file filename.txt
Get-Content filename.txt
gc filename.txt
type filename.txt

0バイトの空ファイルを作成する

New-Item fileName
ni fileName

ファイルを作成し文字列の書き込みを行う

New-Item -Path . -Name "fileName.txt" -ItemType "file" -Value "some string"

ファイルに文字列を追記する

Add-Content filename.txt addsomestring
ac  filename.txt "あいえうえお"

ファイルの中身を設定する(既存内容を指定した内容で置き換え)

Set-Content fileName.txt "some string"
sc fileName.txt "some string"

ファイルやディレクトリをコピーする

cp fromitem toitem
Copy-Item fromitem toitem
cpi fromitem toitem
copy fromitem toitem

ファイルやディレクトの名前を変更する

Rename-Item fromname newname
rni fromname newname
ren fromname newname

ファイルやディレクトリの削除

rm itemname
rmdir itemname
Remove-Item itemname
ri itemname
del itemname
erase itemname
rd itemname

ファイルやディレクトリの移動

mv fromitem toitem
Move-Item fromitem toitem
mi fromitem toitem
move fromitem toitem

サービスとプロセス

サービスを一覧で表示する

Get-Service
gsv

名称を指定してサービスを表示する

Get-Service Dhcp
gsv Dhcp

サービスを開始する

Start-Service Dhcp
sasv Dhcp

サービスを停止する

Stop-Service Dhcp
spsv Dhcp

サービスを再起動する

Restart-Service Dhcp

プロセスの一覧を取得する

Get-Process
ps

プロセスを名称で取得する

Get-Process svchost
ps svchost

新規プロセスを開始する

start a.txt
Start-Process b.xlsx
saps c.html

プロセスを停止する

Stop-Process 123
spps 123
kill 123

ネットワーク

HTTPリクエストの結果を取得する

wget example.com
curl example.net
Invoke-WebRequest example.org
iwr example.info

.net objectのよびだし

基本構文、制御

if,elseif,else

if ($condition) {
}
elseif ($condition) {
}
else {
}

switch

switch ($v) {
    1 {  }
    2 {  }
    Default {  }
}

foreach

foreach ($item in $array) {
}

for

for ($i = 0; $i -lt $array.length;$i++) {
}

do-while

do {
} while ($condition)

do-until

do {
} until ($condition)

while

while ($condition) {}  

try / catch

try{
}catch{
}finally{
}

function

function sample(){
}

参考ページ