Open19
PowerShell - Tips
copy は xcopy
copy (Copy-Item) は、ネイティブコマンドの copy ではなく xcopy に該当する。
パスとして正しい入力があるまでプロンプトを繰り返す
do {
$FolderPath = Read-Host -Prompt "Enter the root folder path"
} while (-not (Test-Path -Path $FolderPath -PathType Container))
「tail -f」の代替。ファイルをUTF8で開き、末尾100行を表示後、ファイルに追記があればその内容もリアルタイムで表示
Get-Content "log.log" -Wait -last 100 -Encoding UTF8
1秒あたり1156キロバイトの接続速度で30.18メガバイトのファイルをダウンロードするのに要する時間(秒単位)を計算する例。
30.18mb / 1156kb
26.7338408304498
平均、合計、最小、最大を出す例。
1..10 | Measure-Object -Average -Sum -Minimum -Maximum
Count : 10
Average : 5.5
Sum : 55
Maximum : 10
Minimum : 1
StandardDeviation :
Property :
ImportExcel
xlsxを扱いたいのでインストールする。
Install-Module -Name ImportExcel -Scope CurrentUser
-AutoSize を使用すると、Ubuntuでは警告が出た。
WARNING: ImportExcel Module Cannot Autosize. Please run the following command to install dependencies:
apt-get -y update && apt-get install -y --no-install-recommends libgdiplus libc6-dev
WARNING: Auto-fitting columns is not available with this OS configuration.
以下、インストールしてみる。
sudo apt-get -y update && apt-get install -y --no-install-recommends libgdiplus libc6-dev
どうしても使えないらしい。
WARNING: Auto-fitting columns is not available with this OS configuration.
英文のテキストファイルから単語の頻度を抽出
テキストファイルの読み込み。
$content = Get-Content -Path ./test.txt -Raw
空白で分割して配列に入れる。その際、空文字は対象外とする。
$words = $content -split '\s+' | Where-Object { $_ -ne "" }
Group-Objectで単語の出現回数を出し、Sort-Objectで降順にする。
$wordFrequencies = $words | Group-Object | Sort-Object -Property Count -Descending
コロンと半スペ自動付加
Read-Hostに-Promptオプションを付けると、コロン+半スペが自動付加される。
$FolderPath = Read-Host -Prompt "Dir"
Dir:
正規表現でマッチ
"hello world" -match "^.+llo .+d$"
正規表現で置換
"Hello World" -replace '(.*) (.*)','$2 $1'
ワイルドカードでマッチ
"hello world" -like "*world"
連番を出力
この書き方好き。
1..5 | % {
Write-Host $_
}
もちろん一行でもいける。
1..5 | % {Write-Host $_}
1
2
3
4
5
これを応用し、連番フォルダを作成
ゼロ埋め
mkdir (1..5 | % { "folder_{0:00}" -f $_})
単なる連番フォルダであれば、以下で作成できる。
mkdir (1..99)
ビープ音
ピーッ※音量注意
[console]::beep(740, 1000)
喋らせる
Add-Type -AssemblyName System.Speech
$synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$synthesizer.Speak('やあ、元気?')
OS情報を得る
※PowerShell 7.0以降
Get-CimInstance -ClassName CIM_OperatingSystem
SystemDirectory Organization BuildNumber RegisteredUser SerialNumber Version
--------------- ------------ ----------- -------------- ------------ -------
C:\Windows\system32 <ビルド番号> <User> <シリアル番号> <OSバージョン>マシン情報を得る
※PowerShell 7.0以降
Get-CimInstance -ClassName CIM_ComputerSystem
Name PrimaryOwnerName Domain TotalPhysicalMemory Model Manufacturer
---- ---------------- ------ ------------------- ----- ------------
<マシン名> <User> <ドメイン> <メモリ容量> <マシンモデル名> <メーカー>グリッドビューで表示する
Get-Process | Out-GridView
Out-GridViewのウィンドウには、フィルターボックスがあります。このボックスに文字列や条件を入力すると、表示されるデータがその条件に一致するものだけになります。
grepした結果を表示すると便利かも。
Get-ChildItem -Recurse -Filter '*.ps1' | Select-String -Pattern 'ASCII' | Out-GridView
これを応用して書いたローカルディスクの使用状況を表示するスクリプト
https://gist.github.com/KentaGoto/d861b05798963a6659f6d296a671bbda
テキストファイル内の文言を行単位でユニークにする
Get-Content input.txt | Sort-Object -Unique | Set-Content output.txt