3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Powershell備忘録(応用)

Last updated at Posted at 2022-11-30

初めに

リストの操作やファイル入出力等,実際にpowershellによる処理方法を記録する。
ググったら出てくるものの備忘録

スクリプトの実行を許可

Windows10では、powershellのスクリプトファイルの実行はデフォルトで許可されていない。
powershellを管理者権限で開き、以下の命令を実行する。

Set-ExecutionPolicy RemoteSigned

ファイルをUTF-8で保存

デフォルトで保存すると、UTF-16で保存される。
UTF-8で保存したいときに。

[System.IO.File]::WriteAllLines($saveFname, $list_URL)

ファイルのリネーム

フォルダ内のファイルを一括で変更したいときに
例として、フォルダDIR内のmp3ファイルを一括リネームする。

$items = Get-ChildItem -path "$DIR\*.mp3"

foreach($item in $items){
    $fname=$item.Name
    #Write-Host $DIR"  "$item
    #Write-Host "$DIR_$fname"
    $newName = "$DIR"+"_"+$fname
    Rename-Item -LiteralPath $item  -newname $newName

Get-ChildItemでフォルダ内のmp3ファイルを全て取得。
$item.Nameでファイル名のみを取得($itemのみだと絶対パスが帰ってくる)、$newNameに新しいファイル名を作成後、Rename-Itemでファイル名を変更する。
  -LiteralPathを付けることで、スペースの入ったファイル名も変更可能。

ファイルの移動

フォルダ内のファイルを一括で移動したいときに
例として、フォルダDIR内のmp3ファイルをDIR2に移動する。

Move-Item -path .\DIR\*.mp3 -destination (Convert-Path .\DIR2)

Start-process

powershellから別のプログラムを実行する。
メモ帳等のアプリを別ウィンドウで開くことが可能。

PowershellからPowershellを実行する

以下の書き方でも実行できるが,同期実行になる(このスクリプトの処理が全て終わるるのを待つ).

.\other_script.ps1

非同期で行う場合,以下の書き方で実行できる.(別のPowershellウィンドウが開く).

Start-Process powershell.exe -ArgumentList  "-file .\other_script.ps1 "

-waitを使うと、Start-Processを同期で行う。
-PassThruを使うとプロセスIDを取得できる.
実行したアプリを消すとき等に利用できる.

$pid = Start-Process powershell.exe -ArgumentList  "-file .\other_script.ps1 " -PassThru
#プロセスIDを取得
Write-Host $pid_knock.id
#別スクリプトの停止Stop-Process -Id $pid.id

リスト操作

リストから任意の位置を削除・追加

リストを指定した位置で分割,要素を足すor消したのち結合することで実装.

function addItem_FromList($arr,$index,$item){
    $arr_bef=$arr[0..$index]
    $arr_aft=$arr[($index+1)..$arr.Length]
    #$arr_bef[$arr.Length]=$item
    $arr_bef+=$item
    return $arr_bef+$arr_aft
}
function removeItem_FromList($arr,$index){
    $arr_bef=$arr[0..($index-1)]
    $arr_aft=$arr[($index+1)..$arr.Length]
    return $arr_bef+$arr_aft
}

ログファイルを出力

日時付きでログをテキストに出力する.
write-OutputAdd-Contentでテキストファイルに追記できる.
1つ目のwrite-Outputで日時を追記,2行目のwrite-Outputでログを追記する.

function writeLogFile($text){
    $LogFile="searchLog.txt"
    Write-Output (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Add-Content $LogFile -Encoding UTF8
    Write-Output $text | Add-Content $Name_LogFile -Encoding UTF8
}

ファイル探索

Get-ChildItemで特定のファイルを探索

Get-ChildItem -filter A.txt -Recurse

-filterでマッチするファイル名を探索。*を使ってワイルドカードにできる。
-Recurseを使って再帰的に検索する。

Get-ChildItemから特定のパスを含む結果を除外する

Get-ChildItemで取得したアイテムをWhere-Objectを使った正規表現ではじく.
$_.FullNameでアイテムの絶対パスが取得,-notmatchによって一致しないアイテムを取得

$dirs_exclude= ".*(node_modules|hogehoge|fuga\\fugafuga|\.mp4).*"
$fileItems=Get-ChildItem -Recurse -File  | Where-Object {$_.FullName -notmatch $dirs_exclude}

ファイル出力(コンソール出力)

Write-Hostだと出力されない。Write-Outputだとリダイレクトされる。
Write-Hostはコンソールへ出力されるため、パイプ処理に渡すことができず、ファイルへ出力できない。
参考:Write-Host、Write-Output 、echo の使い分けはとても重要

Write-Host "hello world from Host" > test.txt
Write-Output "hello world from Output" > test.txt
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?