#amazon(4873113822)
#amazon(477415542X)
#adsense(728x90)
- cmd.exe に代わる Windows 用のシェル、スクリプト言語
- Windows Script Host の後継
- Windows 7、Windows Server 2008 R2 は PowerShell 2.0 が標準でインストールされている
- それ以前の OS は別途インストールする
- Windows PowerShell
- Windows PowerShell ISE [Integrated Scripting Environmentnt]
- デフォルトでは .ps1 ファイルをダブル クリックするとメモ帳が起動する
- レジストリの HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell の値が Open になっている
- Open は notepad.exe に関連付けられているため、powershell.exe に関連付けられている 0 に変更するとダブル クリックでスクリプトが実行される
if ( expr1 )
{
command1
}
else
{
command3
}
条件式 (expression)†[edit]
条件式 | 意味 | 説明 |
-d file | directory | file がディレクトリなら真 |
-f file | file | file がファイルなら真 |
-e file | | file が存在すれば真 |
-r file | read | file が存在して、読み込み可能なら真 |
-w file | write | file が存在して、書き込み可能なら真 |
-x file | excute | file が存在して、実行可能なら真 |
-s file | size | file が存在して、サイズが 0 でなければ真 |
条件式 | 説明 |
str1 = str2 | str1 と str2 が等しければ真 |
str1 != str2 | str1 と str2 が等しくなければ真 |
-z str | str が空なら真 |
-n str str | str が空でなければ真 |
条件式 | 意味 | 説明 |
num1 -eq num2 | equal | num1 と num2 が等しければ真 |
num1 -ne num2 | not equal | num1 と num2 が等しくなければ真 |
num1 -gt num2 | greater than | num1 が num2 超なら真 |
num1 -lt num2 | less than | num1 が num2 未満なら真 |
num1 -ge num2 | less than or equal | num1 が num2 以上なら真 |
num1 -le num2 | greater than or equal | num1 が num2 以下なら真 |
条件式 | 説明 |
$var -is type | 型が正しければ真 |
$var -isnot type | 型が正しくなければ偽 |
条件式 | 説明 |
Test-Path path [-PathType Leaf|Container] | path が存在する場合は真 Leaf … ファイル指定 Container … ディレクトリ指定 |
Test-Connection host | host の PING 応答がある場合は真 |
- 条件式の条件式
条件式 | 意味 | 説明 |
! expr | -not | exprが偽なら真 |
expr1 -and expr2 | and | expr1 と expr2 が両方真なら真 |
expr1 -or expr2 | or | expr1 か expr2 のどちらかが真なら真 |
変数 (variable)†[edit]
変数 | 説明 |
$args[num] | num 番目の引数 |
$? | 直前のコマンドの終了コード |
$null | Null |
$true | 真 |
$false | 偽 |
$_ $input | パイプで渡されたオブジェクト |
Get-Command†[edit]
- コマンドレットの一覧を出力する
Get-Command
Get-Alias†[edit]
Set-ExecutionPolicy†[edit]
ポリシー | 説明 |
Restricted | すべてのスクリプトの実行を禁止 (Windows Server 2012 までのデフォルト) |
AllSigned | すべてのスクリプトに証明書を要求 |
RemoteSigned | インターネットでダウンロードしたスクリプトのみ証明書を要求 (Windows Server 2012 R2 以降のデフォルト) |
Unrestricted | すべてのスクリプト実行を許可 インターネットでダウンロードしたスクリプトは確認画面が出る |
Bypass | すべてのスクリプト実行を許可 |
Get-History (history)†[edit]
- コマンドレットのヘルプを出力する
Get-Help Cmdlet
Get-Process (ps)†[edit]
- プロセスの一覧を出力する
Get-Process [option]
オプション | 説明 |
-Id | プロセス ID |
-Name | プロセス名 ワイルドカード、カンマ区切りで複数指定可能 |
Start-Process†[edit]
Stop-Process (kill)†[edit]
オプション | 説明 |
-Id | PID |
-Name | プロセス名 ワイルドカード、カンマ区切りで複数指定可能 |
-Confirm | 確認を要求する |
Get-Location (pwd)†[edit]
Set-Location (cd)†[edit]
Get-ChildItem (ls)†[edit]
- ex)
PS C:\Users\user> Get-ChildItem↵
ディレクトリ: C:\Users\user
Mode LastWriteTime Length Name
---- ------------- ------ ----
d-r-- 2013/02/07 16:38 Contacts
d-r-- 2013/03/22 19:24 Desktop
d-r-- 2013/03/11 18:35 Documents
d-r-- 2013/03/22 17:34 Downloads
d-r-- 2013/02/07 16:38 Favorites
d-r-- 2013/02/26 11:26 Links
d-r-- 2013/02/07 16:38 Music
d---- 2013/03/01 11:07 Oracle
d-r-- 2013/02/07 16:38 Pictures
d-r-- 2013/02/07 16:38 Saved Games
d-r-- 2013/02/07 16:38 Searches
d-r-- 2013/02/07 16:38 Videos
- ex)
PS C:\> Get-ChildItem -Filter "*.txt"↵
- ex) C:\dir 配下のすべての Thumbs.db を削除する
PS C:\> Get-ChildItem -Path C:\dir\ -Include Thumbs.db -Recurse -Force | Remove-Item -Force
Move-Item (mv)†[edit]
Copy-Item (cp)†[edit]
Remove-Item (rm)†[edit]
PS C:\> Remove-Item file [option]
オプション | 説明 |
-Recurse | 確認なしに再帰的に削除する |
-Force | 非表示、読み取り専用属性ファイルを削除する |
-Confirm | 確認を表示する |
-WhatIf | 実際の削除は行わず、処理内容を出力する |
mkdir (Function)†[edit]
Write-Output (echo)†[edit]
- 標準出力に出力する
[Write-Output] object
- ex) 文字列を出力する
PS C:\> Write-Output "aaa"
aaa
PS C:\> Write-Output "aaa" "bbb"
aaa
bbb
PS C:\> Write-Output "aaa","bbb"
aaa
bbb
- ex) Write-Output を省略することも出来る
PS C:\> "aaa"
aaa
PS C:\> "aaa","bbb"
aaa
bbb
- ex) 省略した場合、複数要素をスペースで区切ることは出来ない
PS C:\> "aaa" "bbb"
Unexpected token 'bbb' in expression or statement.
At line:1 char:12
+ "aaa" "bbb" <<<<
+ CategoryInfo : ParserError: (bbb:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Get-Content (cat, tail, wc -l)†[edit]
Select-String (=grep)†[edit]
PS C:\> Select-String [-Pattern] pattern [-Path] filename
PS C:\> Get-Content path | Select-String [-Pattern] pattern
オプション | 説明 |
-NotMatch | pattern に不一致な行を出力 |
-CaseSensitive | |
-Quiet | 出力内容がある場合は真、ない場合は偽を返す |
Sort-Object (sort)†[edit]
オプション | 意味 |
-CaseSensitive | 大文字小文字を区別する |
-Descending | 降順で並び替える |
-Unique | 重複を排除する (Get-Unique と同じ) |
-property property | 並べ替え対象のプロパティを指定する |
-last num | 最後の n 行を出力する |
Set-ItemProperty (touch)†[edit]
PS C:\> Set-ItemProperty path -Name property -Value time↵
- property
- CreationTime
- LastWriteTime
- time
- ex) C:\dir\ 直下のファイル/フォルダの作成日時を設定する
PS C:\> Set-ItemProperty C:\dir\ -Name CreationTime -Value "2022/01/01 00:00:00"↵
- ex) C:\dir\ 配下すべてのファイル/フォルダの更新日時を設定する
PS C:\> Get-ChildItem -Recurse C:\dir\ | ForEach-Object {Set-ItemProperty $_.FullName -Name LastWriteTime -Value "2022/03/07 00:00:00"}↵
Get-Date (date)†[edit]
オプション | 説明 |
-Format format | 表示形式を指定する |
フォーマット | 説明 |
yyyy | 西暦 4 桁 |
MM | 月 2 桁 |
MMM | 月 英語 3 文字 |
MMMM | 月 英語 |
dd | 日 2 桁 |
ddd | 曜日 英語 3 文字 |
dddd | 曜日 英語 |
HH | 時 2 桁 (24 時間表記) |
HH | 時 2 桁 (12 時間表記) |
mm | 分 2 桁 |
ss | 秒 2 桁 |
| 説明 |
(Get-Date).dayofyear | 1 年の何日目かを出力 |
(Get-Date).ToUniversalTime() | UTC 時間に変換する |
(Get-Date).AddSeconds(n) | n 秒先を出力する (マイナスだと遡る) |
(Get-Date).AddMinutes(n) | n 分先を出力する (マイナスだと遡る) |
(Get-Date).AddHours(n) | n 時間先を出力する (マイナスだと遡る) |
(Get-Date).AddDays(n) | n 日先を出力する (マイナスだと遡る) |
(Get-Date).AddMonths(n) | n ヶ月先を出力する (マイナスだと遡る) |
(Get-Date).AddYears(n) | n ヶ年先を出力する (マイナスだと遡る) |
Add-Content†[edit]
Start-Sleep (sleep)†[edit]
.NET Framework Method†[edit]
- ex) スペースで分割する
PS C:\> "aa bb cc".Split()
aa
bb
cc
- ex) スペースで分割した上で、空白行を削除する
PS C:\> "aa bb cc".Split(,[StringSplitOptions]::RemoveEmptyEntries)
aa
bb
cc
- ex) カンマで分割する
PS C:\> "aa,bb,cc".Split(",")
aa
bb
cc
ToUpper, ToLower†[edit]
#adsense(728x90)