| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Powershell」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Powershell== ==コマンドレット== *http://technet.microsoft.com/ja-jp/library/dn169200.aspx #get-command #get-help get-command #get-command | select-string…」)
 
 
(同じ利用者による、間の18版が非表示)
1行目: 1行目:
==Powershell==
+
==[[Powershell]]==
 +
===引数===
 +
*https://microsoftou.com/ps-arguments/
 +
*$Args[]に格納
 +
*Param($hoge1,$hoge2) とすることで、指定した変数に設定可
 +
 
 +
===ファイル作成===
 +
New-Item c:\temp\myfile.txt -type file
 +
 
 +
===echo===
 +
*Write-Outputのエイリアス
 +
 
 +
===ファイル書き出し===
 +
*https://bayashita.com/p/entry/show/90
 +
<pre>
 +
# バッククオテーションでエスケープ
 +
$str = "`"abc`""
 +
 +
Write-Host $str
 +
 +
# ヒアドキュメントの場合はエスケープ不要
 +
$str = @"
 +
ヒアドキュメントの場合は " はそのまま
 +
記述できます。
 +
"@
 +
</pre>
 +
 
 
==コマンドレット==
 
==コマンドレット==
 
*http://technet.microsoft.com/ja-jp/library/dn169200.aspx
 
*http://technet.microsoft.com/ja-jp/library/dn169200.aspx
77行目: 103行目:
 
|
 
|
 
|-
 
|-
|Get-Help
+
|Get-[[Help]]
 
|
 
|
 
|-
 
|-
104行目: 130行目:
 
|
 
|
 
|-
 
|-
|Get-PSProvider
+
|Get-PSPro[[vi]]der
 
|
 
|
 
|-
 
|-
|Get-Service
+
|Get-Ser[[vi]]ce
 
|
 
|
 
|-
 
|-
167行目: 193行目:
 
|
 
|
 
|-
 
|-
|Read-Host
+
|[[R]]ead-Host
 
|
 
|
 
|-
 
|-
|Remove-Item
+
|[[R]]emove-Item
 
|
 
|
 
|-
 
|-
|Remove-PSDrive
+
|[[R]]emove-PSDrive
 
|
 
|
 
|-
 
|-
|Rename-Item
+
|[[R]]ename-Item
 
|
 
|
 
|-
 
|-
|Restart-Service
+
|Restart-Ser[[vi]]ce
 
|
 
|
 
|-
 
|-
|Resume-Service
+
|Resume-Ser[[vi]]ce
 
|
 
|
 
|-
 
|-
206行目: 232行目:
 
|
 
|
 
|-
 
|-
|Set-Service
+
|Set-Ser[[vi]]ce
 
|
 
|
 
|-
 
|-
212行目: 238行目:
 
|
 
|
 
|-
 
|-
|Start-Service
+
|Start-Ser[[vi]]ce
 
|
 
|
 
|-
 
|-
221行目: 247行目:
 
|
 
|
 
|-
 
|-
|Stop-Service
+
|Stop-Ser[[vi]]ce
 
|
 
|
 
|-
 
|-
|Suspend-Service
+
|Suspend-Ser[[vi]]ce
 
|
 
|
 
|-
 
|-
245行目: 271行目:
  
 
===[http://technet.microsoft.com/ja-jp/library/ee176842.aspx Get-Command]===
 
===[http://technet.microsoft.com/ja-jp/library/ee176842.aspx Get-Command]===
*すべての Windows PowerShell コマンドレットの一覧を返す
+
*すべての [[Windows]] PowerShell コマンドレットの一覧を返す
 
===[http://technet.microsoft.com/ja-jp/library/ee176848.aspx Get-Help]===
 
===[http://technet.microsoft.com/ja-jp/library/ee176848.aspx Get-Help]===
*PowerShell には、コマンド ヘルプと概念に関するヘルプの 2 種類のヘルプがある
+
*PowerShell には、コマンド ヘルプと[[概念]]に関するヘルプの 2 種類のヘルプがある
 
====使用できるすべてのヘルプ トピック一覧を取得====
 
====使用できるすべてのヘルプ トピック一覧を取得====
  Get-Help *
+
  Get-[[Help]] *
====トピック名を Get-Help に渡すことで特定のトピックに関するヘルプを取得できる====
+
====トピック名を Get-[[Help]] に渡すことで特定のトピックに関するヘルプを取得できる====
  Get-Help Get-Member | more
+
  Get-[[Help]] Get-Member | more
  
 
===[http://technet.microsoft.com/ja-jp/library/ee176956.aspx Select-String]===
 
===[http://technet.microsoft.com/ja-jp/library/ee176956.aspx Select-String]===
 
*文字列値の存在確認
 
*文字列値の存在確認
 
*エイリアス sls
 
*エイリアス sls
<blockquote>grepの代替として</blockquote>
+
&lt;blockquote&gt;grepの代替として&lt;/blockquote&gt;
  
 
*grep で使えるようにしておく
 
*grep で使えるようにしておく
 
  Set-Alias grep Select-String
 
  Set-Alias grep Select-String
 
===[http://technet.microsoft.com/ja-jp/library/ee176839.aspx Get-Alias]===
 
===[http://technet.microsoft.com/ja-jp/library/ee176839.aspx Get-Alias]===
*Windows PowerShell エイリアスのリストを返す
+
*[[Windows]] PowerShell エイリアスのリストを返す
 
*エイリアス gal
 
*エイリアス gal
 
===書式===
 
===書式===
269行目: 295行目:
 
====Format-Wide ====
 
====Format-Wide ====
 
*オブジェクトの既定のプロパティだけが表示されます
 
*オブジェクトの既定のプロパティだけが表示されます
  PS> Get-Process -Name powershell | Format-Wide
+
  PS&gt; Get-Process -Name powershell | Format-Wide
 
====Format-List ====
 
====Format-List ====
 
*リスト形式で表示されます。各プロパティがその名前と共に別々の行に表示されます
 
*リスト形式で表示されます。各プロパティがその名前と共に別々の行に表示されます
  PS> Get-Process -Name powershell | Format-List
+
  PS&gt; Get-Process -Name powershell | Format-List
 
====Format-Table ====
 
====Format-Table ====
  PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company
+
  PS&gt; Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company
 
==コマンドサンプル==
 
==コマンドサンプル==
 
*[http://qiita.com/cd01/items/da9a36582372e7d0a7f6 「 Linux ならできるのに、だから Windows は...」「それ PowerShell でできるよ」]
 
*[http://qiita.com/cd01/items/da9a36582372e7d0a7f6 「 Linux ならできるのに、だから Windows は...」「それ PowerShell でできるよ」]
283行目: 309行目:
 
====フォルダ配下のファイル名の検索====
 
====フォルダ配下のファイル名の検索====
 
*[https://technet.microsoft.com/ja-jp/library/ee177028.aspx Where-Object]  
 
*[https://technet.microsoft.com/ja-jp/library/ee177028.aspx Where-Object]  
  > get-childitem | where-object {$_.name -like "Python*"}
+
  &gt; get-childitem | where-object {$_.name -like "[[Python]]*"}
==Powershell ISE==
+
==[[Powershell]] ISE==
===Windows8===
+
===[[Windows]]8===
 
*[http://operationslab.wordpress.com/2013/02/13/%E3%82%B9%E3%82%BF%E3%83%BC%E3%83%88%E7%94%BB%E9%9D%A2%E3%81%AB-powershell-ise-%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B/ Windows 8 のデフォルト設定では PowerShell ISE がスタート画面に表示されません。また、アプリの検索を行っても出てきません。]
 
*[http://operationslab.wordpress.com/2013/02/13/%E3%82%B9%E3%82%BF%E3%83%BC%E3%83%88%E7%94%BB%E9%9D%A2%E3%81%AB-powershell-ise-%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B/ Windows 8 のデフォルト設定では PowerShell ISE がスタート画面に表示されません。また、アプリの検索を行っても出てきません。]
 
*[http://pc-karuma.net/windows8-powershell-ise-start/ 「PowerShell ISE」には、補完、デバッグ機能などの便利な機能が付いている]
 
*[http://pc-karuma.net/windows8-powershell-ise-start/ 「PowerShell ISE」には、補完、デバッグ機能などの便利な機能が付いている]
291行目: 317行目:
 
[[File:0971_powershell_ise.jpg]]
 
[[File:0971_powershell_ise.jpg]]
  
==Tips==
+
==Linuxコマンド==
 +
{|class="wikitable"
 +
!Linuxコマンド!!Powershellコマンド
 +
|-
 +
|env||Get-ChildItem Env:
 +
|-
 +
|grep||Select-String
 +
|-
 +
|ls||Get-ChildItem
 +
|-
 +
|man||Get-Help
 +
|-
 +
|rm -rf||Remove-Item -Path -Recurse -Force
 +
|}
 +
 
 +
==[[Tips]]==
 
===スクリプトファイルを実行する===
 
===スクリプトファイルを実行する===
 
*http://www.atmarkit.co.jp/ait/articles/0709/20/news125.html
 
*http://www.atmarkit.co.jp/ait/articles/0709/20/news125.html
 
*PowerShellはカレント・フォルダを検索しない
 
*PowerShellはカレント・フォルダを検索しない
  PS > ./hoge.ps1
+
  PS &gt; ./hoge.ps1
 
====スクリプト実行ポリシーを設定====
 
====スクリプト実行ポリシーを設定====
 
*以下のエラーが発生する
 
*以下のエラーが発生する
<blockquote>このシステムではスクリプトの実行が無効になっているため...</blockquote>
+
&lt;blockquote&gt;このシステムではスクリプトの実行が無効になっているため...&lt;/blockquote&gt;
  PS > Set-ExecutionPolicy RemoteSigned
+
  PS &gt; Set-ExecutionPolicy [[R]]emoteSigned
 
{|class="wikitable"
 
{|class="wikitable"
 
!ポリシー
 
!ポリシー
 
!内容
 
!内容
 
|-
 
|-
|Restricted
+
|[[R]]estricted
 
|すべてのスクリプトを実行不可
 
|すべてのスクリプトを実行不可
 
|-
 
|-
310行目: 351行目:
 
|すべてのスクリプトに証明書を要求
 
|すべてのスクリプトに証明書を要求
 
|-
 
|-
|RemoteSigned
+
|[[R]]emoteSigned
 
|インターネット経由でダウンロードしたスクリプトのみ証明書を要求
 
|インターネット経由でダウンロードしたスクリプトのみ証明書を要求
 
|-
 
|-
321行目: 362行目:
 
===環境変数===
 
===環境変数===
 
====一覧表示====
 
====一覧表示====
  PS> get-childitem env:
+
  PS&gt; get-childitem env:
 
====指定表示====
 
====指定表示====
 
  $Env:環境変数名
 
  $Env:環境変数名
 
=====例=====
 
=====例=====
  PS> $env:path
+
  PS&gt; $env:path
 
====設定====
 
====設定====
  PS> set-item env:HOGE hoge
+
  PS&gt; set-item env:HOGE hoge
  
 
===文字列分割===
 
===文字列分割===
 
*環境変数を ";"で分割
 
*環境変数を ";"で分割
  PS> $Env:path -split ";"
+
  PS&gt; $Env:path -split ";"
 
===バックグラウンドジョブの実行と確認===
 
===バックグラウンドジョブの実行と確認===
 
*Start-Job、Get-Job
 
*Start-Job、Get-Job
 
  Start-Job -ScriptBlock{ 実行するコマンド }
 
  Start-Job -ScriptBlock{ 実行するコマンド }
 
=====例=====
 
=====例=====
  PS C:\Programs\wildfly-8.0.0.Final\bin> start-job -ScriptBlock {.\standalone.bat}
+
  PS C:\Programs\wildfly-8.0.0.Final\bin&gt; start-job -ScriptBlock {.\standalone.bat}
 
   
 
   
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
 
  --    ----            -------------  -----        -----------    --------            -------
 
  --    ----            -------------  -----        -----------    --------            -------
  2      Job2            BackgroundJob  Running       True            localhost            .\standalone.bat
+
  2      Job2            BackgroundJob  [[R]]unning       True            localhost            .\standalone.bat
 
   
 
   
 
   
 
   
  PS C:\Programs\wildfly-8.0.0.Final\bin> get-job
+
  PS C:\Programs\wildfly-8.0.0.Final\bin&gt; get-job
 
   
 
   
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
350行目: 391行目:
  
 
===カレントディレクトリをエクスプローラーで開く===
 
===カレントディレクトリをエクスプローラーで開く===
  > explorer (get-location).path
+
  &gt; explorer (get-location).path
  
 
===コマンド情報の確認(whereis)===
 
===コマンド情報の確認(whereis)===
  PS C:\workspaces\vscode\reactlesson> get-command git
+
  PS C:\workspaces\vscode\reactlesson&gt; get-command git
 
   
 
   
 
  CommandType    Name                                              Version    Source
 
  CommandType    Name                                              Version    Source
 
  -----------    ----                                              -------    ------
 
  -----------    ----                                              -------    ------
  Application    git.exe                                            2.12.2.2  C:\Program Files\Git\cmd\git.exe
+
  Application    git.exe                                            2.12.2.2  C:\Program Files\[[Git]]\cmd\git.exe
 +
 
 +
===フォルダのサイズを調べる===
 +
----
 +
<pre>
 +
$fso = new-object -com Scripting.FileSystemObject
 +
gci "c:\windows\" -Directory `
 +
  | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName `
 +
  | sort Size -Descending `
 +
  | ft @{l='Size [MB]'; e={'{0:N2}    ' -f ($_.Size / 1MB)}},FullName
 +
</pre>

2021年9月2日 (木) 03:13時点における最新版

Powershell

引数

ファイル作成

New-Item c:\temp\myfile.txt -type file

echo

  • Write-Outputのエイリアス

ファイル書き出し

# バッククオテーションでエスケープ
$str = "`"abc`""
 
Write-Host $str
 
# ヒアドキュメントの場合はエスケープ不要
$str = @"
ヒアドキュメントの場合は " はそのまま
記述できます。
"@

コマンドレット

  1. get-command
  2. get-help get-command
  3. get-command | select-string "select"
コマンドレット 内容
Add-History
Clear-Content
Compare-Object
Convert-Path
ConvertTo-Html
Copy-Item
Export-Alias
Export-Clixml
Export-Csv
Foreach-Object
Format-List
Format-Wide
Get-Acl
Get-Alias
Get-AuthenticodeSignature
Get-ChildItem
Get-Command
Get-Content
Get-Culture
Get-Date
Get-Eventlog
Get-ExecutionPolicy
Get-Help
Get-History
Get-Host
Get-Item
Get-ItemProperty
Get-Location
Get-Member
Get-Process
Get-PSDrive
Get-PSProvider
Get-Service
Get-Unique
Get-WMiObject
Group-Object
Import-Alias
Import-Clixml
Import-Csv
Invoke-Expression
Invoke-History
Invoke-Item
Measure-Command
Measure-Object
Move-Item
New-Alias
New-Item
New-PSDrive
New-Timespan
Out-File
Out-Host
Output-Printer
Read-Host
Remove-Item
Remove-PSDrive
Rename-Item
Restart-Service
Resume-Service
Select-Object
Select-String
Set-Alias
Set-Content
Set-Date
Set-ExecutionPolicy
Set-Location
Set-Service
Sort-Object
Start-Service
Start-Sleep
Stop-Process
Stop-Service
Suspend-Service
Tee-Object
Test-Path
Where-Object 返されるデータのフィルタ処理
Write-Host
Write-Warning

Get-Command

  • すべての Windows PowerShell コマンドレットの一覧を返す

Get-Help

  • PowerShell には、コマンド ヘルプと概念に関するヘルプの 2 種類のヘルプがある

使用できるすべてのヘルプ トピック一覧を取得

Get-Help *

トピック名を Get-Help に渡すことで特定のトピックに関するヘルプを取得できる

Get-Help Get-Member | more

Select-String

  • 文字列値の存在確認
  • エイリアス sls

<blockquote>grepの代替として</blockquote>

  • grep で使えるようにしておく
Set-Alias grep Select-String

Get-Alias

  • Windows PowerShell エイリアスのリストを返す
  • エイリアス gal

書式

  • 特定のオブジェクトについて、表示するプロパティを制御するための一連のコマンドレットが用意されています
  • これらすべてのコマンドレットの名前は、Format という動詞で始まります
  • Format 系のコマンドレットには、Format-Wide、Format-List、Format-Table、および Format-Custom があります

Format-Wide

  • オブジェクトの既定のプロパティだけが表示されます
PS> Get-Process -Name powershell | Format-Wide

Format-List

  • リスト形式で表示されます。各プロパティがその名前と共に別々の行に表示されます
PS> Get-Process -Name powershell | Format-List

Format-Table

PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company

コマンドサンプル

フォルダ配下のファイル名の検索

> get-childitem | where-object {$_.name -like "Python*"}

Powershell ISE

Windows8

0971 powershell ise.jpg

Linuxコマンド

Linuxコマンド Powershellコマンド
env Get-ChildItem Env:
grep Select-String
ls Get-ChildItem
man Get-Help
rm -rf Remove-Item -Path -Recurse -Force

Tips

スクリプトファイルを実行する

PS > ./hoge.ps1

スクリプト実行ポリシーを設定

  • 以下のエラーが発生する

<blockquote>このシステムではスクリプトの実行が無効になっているため...</blockquote>

PS > Set-ExecutionPolicy RemoteSigned
ポリシー 内容
Restricted すべてのスクリプトを実行不可
AllSigned すべてのスクリプトに証明書を要求
RemoteSigned インターネット経由でダウンロードしたスクリプトのみ証明書を要求
Unrestricted すべてのスクリプト実行を許可(ただしインターネット経由でダウンロードしたコードは実行確認のみあり)


環境変数

一覧表示

PS> get-childitem env:

指定表示

$Env:環境変数名
PS> $env:path

設定

PS> set-item env:HOGE hoge

文字列分割

  • 環境変数を ";"で分割
PS>  $Env:path -split ";"

バックグラウンドジョブの実行と確認

  • Start-Job、Get-Job
Start-Job -ScriptBlock{ 実行するコマンド }
PS C:\Programs\wildfly-8.0.0.Final\bin> start-job -ScriptBlock {.\standalone.bat}

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Running       True            localhost            .\standalone.bat


PS C:\Programs\wildfly-8.0.0.Final\bin> get-job

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Failed        False           localhost            .\standalone.bat

カレントディレクトリをエクスプローラーで開く

> explorer (get-location).path

コマンド情報の確認(whereis)

PS C:\workspaces\vscode\reactlesson> get-command git

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     git.exe                                            2.12.2.2   C:\Program Files\Git\cmd\git.exe

フォルダのサイズを調べる


$fso = new-object -com Scripting.FileSystemObject
gci "c:\windows\" -Directory `
  | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName `
  | sort Size -Descending `
  | ft @{l='Size [MB]'; e={'{0:N2}    ' -f ($_.Size / 1MB)}},FullName