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

MyMemoWiki

「Powershell」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
12行目: 12行目:
  
 
===ファイル書き出し===
 
===ファイル書き出し===
*https://qiita.com/TAKA-SHU/items/d4467583c62d9b556fa4
+
*https://bayashita.com/p/entry/show/90
 +
<pre>
 +
# バッククオテーションでエスケープ
 +
$str = "`"abc`""
 +
 +
Write-Host $str
 +
 +
# ヒアドキュメントの場合はエスケープ不要
 +
$str = @"
 +
ヒアドキュメントの場合は " はそのまま
 +
記述できます。
 +
"@
 +
</pre?
  
 
==コマンドレット==
 
==コマンドレット==

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

Powershell

引数

ファイル作成

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

echo

  • Write-Outputのエイリアス

ファイル書き出し

  1. # バッククオテーションでエスケープ
  2. $str = "`"abc`""
  3. Write-Host $str
  4. # ヒアドキュメントの場合はエスケープ不要
  5. $str = @"
  6. ヒアドキュメントの場合は " はそのまま
  7. 記述できます。
  8. "@
  9. </pre?
  10.  
  11. ==コマンドレット==
  12. *http://technet.microsoft.com/ja-jp/library/dn169200.aspx
  13.  
  14. #get-command
  15. #get-help get-command
  16. #get-command | select-string "select"
  17.  
  18. {|class="wikitable"
  19. !コマンドレット
  20. !内容
  21. |-
  22. |Add-History
  23. |
  24. |-
  25. |Clear-Content
  26. |
  27. |-
  28. |Compare-Object
  29. |
  30. |-
  31. |Convert-Path
  32. |
  33. |-
  34. |ConvertTo-Html
  35. |
  36. |-
  37. |Copy-Item
  38. |
  39. |-
  40. |Export-Alias
  41. |
  42. |-
  43. |Export-Clixml
  44. |
  45. |-
  46. |Export-Csv
  47. |
  48. |-
  49. |Foreach-Object
  50. |
  51. |-
  52. |Format-List
  53. |
  54. |-
  55. |Format-Wide
  56. |
  57. |-
  58. |Get-Acl
  59. |
  60. |-
  61. |Get-Alias
  62. |
  63. |-
  64. |Get-AuthenticodeSignature
  65. |
  66. |-
  67. |Get-ChildItem
  68. |
  69. |-
  70. |Get-Command
  71. |
  72. |-
  73. |Get-Content
  74. |
  75. |-
  76. |Get-Culture
  77. |
  78. |-
  79. |Get-Date
  80. |
  81. |-
  82. |Get-Eventlog
  83. |
  84. |-
  85. |Get-ExecutionPolicy
  86. |
  87. |-
  88. |Get-[[Help]]
  89. |
  90. |-
  91. |Get-History
  92. |
  93. |-
  94. |Get-Host
  95. |
  96. |-
  97. |Get-Item
  98. |
  99. |-
  100. |Get-ItemProperty
  101. |
  102. |-
  103. |Get-Location
  104. |
  105. |-
  106. |Get-Member
  107. |
  108. |-
  109. |Get-Process
  110. |
  111. |-
  112. |Get-PSDrive
  113. |
  114. |-
  115. |Get-PSPro[[vi]]der
  116. |
  117. |-
  118. |Get-Ser[[vi]]ce
  119. |
  120. |-
  121. |Get-Unique
  122. |
  123. |-
  124. |Get-WMiObject
  125. |
  126. |-
  127. |Group-Object
  128. |
  129. |-
  130. |Import-Alias
  131. |
  132. |-
  133. |Import-Clixml
  134. |
  135. |-
  136. |Import-Csv
  137. |
  138. |-
  139. |Invoke-Expression
  140. |
  141. |-
  142. |Invoke-History
  143. |
  144. |-
  145. |Invoke-Item
  146. |
  147. |-
  148. |Measure-Command
  149. |
  150. |-
  151. |Measure-Object
  152. |
  153. |-
  154. |Move-Item
  155. |
  156. |-
  157. |New-Alias
  158. |
  159. |-
  160. |New-Item
  161. |
  162. |-
  163. |New-PSDrive
  164. |
  165. |-
  166. |New-Timespan
  167. |
  168. |-
  169. |Out-File
  170. |
  171. |-
  172. |Out-Host
  173. |
  174. |-
  175. |Output-Printer
  176. |
  177. |-
  178. |[[R]]ead-Host
  179. |
  180. |-
  181. |[[R]]emove-Item
  182. |
  183. |-
  184. |[[R]]emove-PSDrive
  185. |
  186. |-
  187. |[[R]]ename-Item
  188. |
  189. |-
  190. |Restart-Ser[[vi]]ce
  191. |
  192. |-
  193. |Resume-Ser[[vi]]ce
  194. |
  195. |-
  196. |Select-Object
  197. |
  198. |-
  199. |Select-String
  200. |
  201. |-
  202. |Set-Alias
  203. |
  204. |-
  205. |Set-Content
  206. |
  207. |-
  208. |Set-Date
  209. |
  210. |-
  211. |Set-ExecutionPolicy
  212. |
  213. |-
  214. |Set-Location
  215. |
  216. |-
  217. |Set-Ser[[vi]]ce
  218. |
  219. |-
  220. |Sort-Object
  221. |
  222. |-
  223. |Start-Ser[[vi]]ce
  224. |
  225. |-
  226. |Start-Sleep
  227. |
  228. |-
  229. |Stop-Process
  230. |
  231. |-
  232. |Stop-Ser[[vi]]ce
  233. |
  234. |-
  235. |Suspend-Ser[[vi]]ce
  236. |
  237. |-
  238. |Tee-Object
  239. |
  240. |-
  241. |Test-Path
  242. |
  243. |-
  244. |Where-Object
  245. |返されるデータのフィルタ処理
  246. |-
  247. |Write-Host
  248. |
  249. |-
  250. |Write-Warning
  251. |
  252. |-
  253. |}
  254.  
  255. ===[http://technet.microsoft.com/ja-jp/library/ee176842.aspx Get-Command]===
  256. *すべての [[Windows]] PowerShell コマンドレットの一覧を返す
  257. ===[http://technet.microsoft.com/ja-jp/library/ee176848.aspx Get-Help]===
  258. *PowerShell には、コマンド ヘルプと[[概念]]に関するヘルプの 2 種類のヘルプがある
  259. ====使用できるすべてのヘルプ トピック一覧を取得====
  260. Get-[[Help]] *
  261. ====トピック名を Get-[[Help]] に渡すことで特定のトピックに関するヘルプを取得できる====
  262. Get-[[Help]] Get-Member | more
  263.  
  264. ===[http://technet.microsoft.com/ja-jp/library/ee176956.aspx Select-String]===
  265. *文字列値の存在確認
  266. *エイリアス sls
  267. <blockquote>grepの代替として</blockquote>
  268.  
  269. *grep で使えるようにしておく
  270. Set-Alias grep Select-String
  271. ===[http://technet.microsoft.com/ja-jp/library/ee176839.aspx Get-Alias]===
  272. *[[Windows]] PowerShell エイリアスのリストを返す
  273. *エイリアス gal
  274. ===書式===
  275. *特定のオブジェクトについて、表示するプロパティを制御するための一連のコマンドレットが用意されています
  276. *これらすべてのコマンドレットの名前は、Format という動詞で始まります
  277. *Format 系のコマンドレットには、Format-Wide、Format-List、Format-Table、および Format-Custom があります
  278. ====Format-Wide ====
  279. *オブジェクトの既定のプロパティだけが表示されます
  280. PS> Get-Process -Name powershell | Format-Wide
  281. ====Format-List ====
  282. *リスト形式で表示されます。各プロパティがその名前と共に別々の行に表示されます
  283. PS> Get-Process -Name powershell | Format-List
  284. ====Format-Table ====
  285. PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company
  286. ==コマンドサンプル==
  287. *[http://qiita.com/cd01/items/da9a36582372e7d0a7f6 「 Linux ならできるのに、だから Windows は...」「それ PowerShell でできるよ」]
  288. *[http://tech.sanwasystem.com/entry/2016/07/05/185717 grepコマンドとPowerShellのsls (Select-String)の比較]
  289. *[https://technet.microsoft.com/ja-jp/scriptcenter/powershell_owner04.aspx Windows PowerShell でのパイプ処理とパイプライン]
  290. *[http://www.atmarkit.co.jp/ait/articles/0808/22/news129.html PowerShellのGet-ChildItemコマンドレットでファイル名の一覧を取得する(基本編)]
  291.  
  292. ====フォルダ配下のファイル名の検索====
  293. *[https://technet.microsoft.com/ja-jp/library/ee177028.aspx Where-Object]
  294. > get-childitem | where-object {$_.name -like "[[Python]]*"}
  295. ==[[Powershell]] ISE==
  296. ===[[Windows]]8===
  297. *[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 がスタート画面に表示されません。また、アプリの検索を行っても出てきません。]
  298. *[http://pc-karuma.net/windows8-powershell-ise-start/ 「PowerShell ISE」には、補完、デバッグ機能などの便利な機能が付いている]
  299. *「ファイル名を指定して実行」で、「powershell_ise」を起動する。
  300. [[File:0971_powershell_ise.jpg]]
  301.  
  302. ==Linuxコマンド==
  303. {|class="wikitable"
  304. !Linuxコマンド!!Powershellコマンド
  305. |-
  306. |env||Get-ChildItem Env:
  307. |-
  308. |grep||Select-String
  309. |-
  310. |ls||Get-ChildItem
  311. |-
  312. |man||Get-Help
  313. |-
  314. |rm -rf||Remove-Item -Path -Recurse -Force
  315. |}
  316.  
  317. ==[[Tips]]==
  318. ===スクリプトファイルを実行する===
  319. *http://www.atmarkit.co.jp/ait/articles/0709/20/news125.html
  320. *PowerShellはカレント・フォルダを検索しない
  321. PS > ./hoge.ps1
  322. ====スクリプト実行ポリシーを設定====
  323. *以下のエラーが発生する
  324. <blockquote>このシステムではスクリプトの実行が無効になっているため...</blockquote>
  325. PS > Set-ExecutionPolicy [[R]]emoteSigned
  326. {|class="wikitable"
  327. !ポリシー
  328. !内容
  329. |-
  330. |[[R]]estricted
  331. |すべてのスクリプトを実行不可
  332. |-
  333. |AllSigned
  334. |すべてのスクリプトに証明書を要求
  335. |-
  336. |[[R]]emoteSigned
  337. |インターネット経由でダウンロードしたスクリプトのみ証明書を要求
  338. |-
  339. |Unrestricted
  340. |すべてのスクリプト実行を許可(ただしインターネット経由でダウンロードしたコードは実行確認のみあり)
  341. |-
  342. |}
  343.  
  344.  
  345. ===環境変数===
  346. ====一覧表示====
  347. PS> get-childitem env:
  348. ====指定表示====
  349. $Env:環境変数名
  350. =====例=====
  351. PS> $env:path
  352. ====設定====
  353. PS> set-item env:HOGE hoge
  354.  
  355. ===文字列分割===
  356. *環境変数を ";"で分割
  357. PS> $Env:path -split ";"
  358. ===バックグラウンドジョブの実行と確認===
  359. *Start-Job、Get-Job
  360. Start-Job -ScriptBlock{ 実行するコマンド }
  361. =====例=====
  362. PS C:\Programs\wildfly-8.0.0.Final\bin> start-job -ScriptBlock {.\standalone.bat}
  363. Id Name PSJobTypeName State HasMoreData Location Command
  364. -- ---- ------------- ----- ----------- -------- -------
  365. 2 Job2 BackgroundJob [[R]]unning True localhost .\standalone.bat
  366. PS C:\Programs\wildfly-8.0.0.Final\bin> get-job
  367. Id Name PSJobTypeName State HasMoreData Location Command
  368. -- ---- ------------- ----- ----------- -------- -------
  369. 2 Job2 BackgroundJob Failed False localhost .\standalone.bat
  370.  
  371. ===カレントディレクトリをエクスプローラーで開く===
  372. > explorer (get-location).path
  373.  
  374. ===コマンド情報の確認(whereis)===
  375. PS C:\workspaces\vscode\reactlesson> get-command git
  376. CommandType Name Version Source
  377. ----------- ---- ------- ------
  378. Application git.exe 2.12.2.2 C:\Program Files\[[Git]]\cmd\git.exe
  379.  
  380. ===フォルダのサイズを調べる===
  381. ----
  382. <pre>
  383. $fso = new-object -com Scripting.FileSystemObject
  384. gci "c:\windows\" -Directory `
  385. | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName `
  386. | sort Size -Descending `
  387. | ft @{l='Size [MB]'; e={'{0:N2} ' -f ($_.Size / 1MB)}},FullName