「Powershell」の版間の差分
ナビゲーションに移動
検索に移動
12行目: | 12行目: | ||
===ファイル書き出し=== | ===ファイル書き出し=== | ||
− | *https:// | + | *https://bayashita.com/p/entry/show/90 |
+ | <pre> | ||
+ | # バッククオテーションでエスケープ | ||
+ | $str = "`"abc`"" | ||
+ | |||
+ | Write-Host $str | ||
+ | |||
+ | # ヒアドキュメントの場合はエスケープ不要 | ||
+ | $str = @" | ||
+ | ヒアドキュメントの場合は " はそのまま | ||
+ | 記述できます。 | ||
+ | "@ | ||
+ | </pre? | ||
==コマンドレット== | ==コマンドレット== |
2021年9月2日 (木) 03:13時点における版
Powershell
引数
- https://microsoftou.com/ps-arguments/
- $Args[]に格納
- Param($hoge1,$hoge2) とすることで、指定した変数に設定可
ファイル作成
- New-Item c:\temp\myfile.txt -type file
echo
- Write-Outputのエイリアス
ファイル書き出し
- # バッククオテーションでエスケープ
- $str = "`"abc`""
- Write-Host $str
- # ヒアドキュメントの場合はエスケープ不要
- $str = @"
- ヒアドキュメントの場合は " はそのまま
- 記述できます。
- "@
- </pre?
- ==コマンドレット==
- *http://technet.microsoft.com/ja-jp/library/dn169200.aspx
- #get-command
- #get-help get-command
- #get-command | select-string "select"
- {|class="wikitable"
- !コマンドレット
- !内容
- |-
- |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-PSPro[[vi]]der
- |
- |-
- |Get-Ser[[vi]]ce
- |
- |-
- |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
- |
- |-
- |[[R]]ead-Host
- |
- |-
- |[[R]]emove-Item
- |
- |-
- |[[R]]emove-PSDrive
- |
- |-
- |[[R]]ename-Item
- |
- |-
- |Restart-Ser[[vi]]ce
- |
- |-
- |Resume-Ser[[vi]]ce
- |
- |-
- |Select-Object
- |
- |-
- |Select-String
- |
- |-
- |Set-Alias
- |
- |-
- |Set-Content
- |
- |-
- |Set-Date
- |
- |-
- |Set-ExecutionPolicy
- |
- |-
- |Set-Location
- |
- |-
- |Set-Ser[[vi]]ce
- |
- |-
- |Sort-Object
- |
- |-
- |Start-Ser[[vi]]ce
- |
- |-
- |Start-Sleep
- |
- |-
- |Stop-Process
- |
- |-
- |Stop-Ser[[vi]]ce
- |
- |-
- |Suspend-Ser[[vi]]ce
- |
- |-
- |Tee-Object
- |
- |-
- |Test-Path
- |
- |-
- |Where-Object
- |返されるデータのフィルタ処理
- |-
- |Write-Host
- |
- |-
- |Write-Warning
- |
- |-
- |}
- ===[http://technet.microsoft.com/ja-jp/library/ee176842.aspx Get-Command]===
- *すべての [[Windows]] PowerShell コマンドレットの一覧を返す
- ===[http://technet.microsoft.com/ja-jp/library/ee176848.aspx Get-Help]===
- *PowerShell には、コマンド ヘルプと[[概念]]に関するヘルプの 2 種類のヘルプがある
- ====使用できるすべてのヘルプ トピック一覧を取得====
- Get-[[Help]] *
- ====トピック名を Get-[[Help]] に渡すことで特定のトピックに関するヘルプを取得できる====
- Get-[[Help]] Get-Member | more
- ===[http://technet.microsoft.com/ja-jp/library/ee176956.aspx Select-String]===
- *文字列値の存在確認
- *エイリアス sls
- <blockquote>grepの代替として</blockquote>
- *grep で使えるようにしておく
- Set-Alias grep Select-String
- ===[http://technet.microsoft.com/ja-jp/library/ee176839.aspx 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
- ==コマンドサンプル==
- *[http://qiita.com/cd01/items/da9a36582372e7d0a7f6 「 Linux ならできるのに、だから Windows は...」「それ PowerShell でできるよ」]
- *[http://tech.sanwasystem.com/entry/2016/07/05/185717 grepコマンドとPowerShellのsls (Select-String)の比較]
- *[https://technet.microsoft.com/ja-jp/scriptcenter/powershell_owner04.aspx Windows PowerShell でのパイプ処理とパイプライン]
- *[http://www.atmarkit.co.jp/ait/articles/0808/22/news129.html PowerShellのGet-ChildItemコマンドレットでファイル名の一覧を取得する(基本編)]
- ====フォルダ配下のファイル名の検索====
- *[https://technet.microsoft.com/ja-jp/library/ee177028.aspx Where-Object]
- > get-childitem | where-object {$_.name -like "[[Python]]*"}
- ==[[Powershell]] ISE==
- ===[[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://pc-karuma.net/windows8-powershell-ise-start/ 「PowerShell ISE」には、補完、デバッグ機能などの便利な機能が付いている]
- *「ファイル名を指定して実行」で、「powershell_ise」を起動する。
- [[File:0971_powershell_ise.jpg]]
- ==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
- *PowerShellはカレント・フォルダを検索しない
- PS > ./hoge.ps1
- ====スクリプト実行ポリシーを設定====
- *以下のエラーが発生する
- <blockquote>このシステムではスクリプトの実行が無効になっているため...</blockquote>
- PS > Set-ExecutionPolicy [[R]]emoteSigned
- {|class="wikitable"
- !ポリシー
- !内容
- |-
- |[[R]]estricted
- |すべてのスクリプトを実行不可
- |-
- |AllSigned
- |すべてのスクリプトに証明書を要求
- |-
- |[[R]]emoteSigned
- |インターネット経由でダウンロードしたスクリプトのみ証明書を要求
- |-
- |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 [[R]]unning 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
- ===フォルダのサイズを調べる===
- ----
- <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
© 2006 矢木浩人