Open13

Windows(PowerShell)用コマンド

BlueSilverCatBlueSilverCat

DcomのAppID

Get-WmiObject Win32_DCOMApplication
Get-WmiObject Win32_DCOMApplication | Where-Object {$_.AppID -eq "{...}"}

デバイスインスタンスパス

Get-PnpDevice | Where-Object {$_.InstanceId -eq "..."}
Get-WmiObject Win32_PNPEntity

CLSID

Get-WmiObject Win32_ClassicCOMClass
Get-WmiObject Win32_ClassicCOMClass | Where-Object {$_.ComponentId -eq "{...}"}

BlueSilverCatBlueSilverCat

System復元

DISM /Online /Cleanup-image /Restorehealth
sfc /verifyonly
or
sfc /scannow
BlueSilverCatBlueSilverCat
  • (Get-ChildItem -LiteralPath "path").Count: 要素の数
  • (Get-ChildItem -LiteralPath "path").Length: 要素(ファイル)のサイズ
  • Split-Path -LiteralPath "path": "-Leaf", "-Parent"などが使えない
BlueSilverCatBlueSilverCat

ゴミ箱へ送る

PowerShell OK
function moveToTrash {
  param (
    [Parameter(Mandatory)] $path,
    [Alias("w")][switch] $whatif
  )
  $path = Get-Item -LiteralPath $path
  # printDelete $path.FullName -str "moveToTrash: "
  if (-not $whatif) {
    $shell = New-Object -ComObject "Shell.Application"
    $trash = $shell.NameSpace(10)
    $trash.MoveHere($path.FullName)
    for ($i = 0; (Test-Path $path.FullName) -and $i -lt 60; ++$i ) {
      Start-Sleep -s 1
    }
  }
}

余談だけど、初めは下記のようにResolve-Pathを使っていて上手くいかなかった

PowerShell NG
function moveToTrash {
  param (
    [Parameter(Mandatory)] $path,
    [Alias("w")][switch] $whatif
  )
  $path = Resolve-Path -LiteralPath $path
  # printDelete $path -str "moveToTrash: "
  if (-not $whatif) {
    $shell = New-Object -ComObject "Shell.Application"
    $trash = $shell.NameSpace(10)
    $trash.MoveHere($path)
    for ($i = 0; (Test-Path $path) -and $i -lt 60; ++$i ) {
      Start-Sleep -s 1
    }
  }
}

解決するには以下のように明示的に$pathの文字列を渡してやる必要がある。

PowerShell OK
function moveToTrash {
  param (
    [Parameter(Mandatory)] $path,
    [Alias("w")][switch] $whatif
  )
  $path = Resolve-Path -LiteralPath $path
  # printDelete $path -str "moveToTrash: "
  if (-not $whatif) {
    $shell = New-Object -ComObject "Shell.Application"
    $trash = $shell.NameSpace(10)
    $trash.MoveHere($path.Path) # $path.ToString()
    for ($i = 0; (Test-Path $path) -and $i -lt 60; ++$i ) {
      Start-Sleep -s 1
    }
  }
}

このような挙動があるからPowerShellは、苦手。
もっとも型とかしっかりと理解できていないからだとは思うけど。

BlueSilverCatBlueSilverCat

New-Item

New-Item -Name $path: ほかのコマンドレットの-Pathに当たる。ワイルドカードを受け付ける。
New-Item $path: ほかのコマンドレットの-LiteralPathに当たる。ワイルドカードを受け付けない。

統一してほしい。

出力の抑制

  • New-Item $path -ItemType Directory | Out-Null
  • $null = New-Item $path -ItemType Directory
  • [void](New-Item $path -ItemType Directory)
BlueSilverCatBlueSilverCat

Oneドライブのディレクトリ移動

  1. 設定 -> このPCのリンク解除
  2. ディレクトリ移動
  3. 再ログイン
  4. 移動先をリンク
BlueSilverCatBlueSilverCat

パーティション 削除

  1. DISKPART
  2. LIST DISK
  3. SELECT DISK n
  4. LIST PARTITION
  5. SELECT PARTITION n
  6. DELETE PARTITION OVERRIDE # 保護されたパーティションの場合は、OverRideを付ける
BlueSilverCatBlueSilverCat

ファイルのアクセス状況を確認する

openfiles を使用する

下準備

ファイルの情報を取得するためには、システム・グローバル・フラグ(OSの動作状態を決めるためのパラメータの1つ)のmaintain objects listをオンする必要がある。
下記を実行するとファイルの情報をトレースできるようになる

openfiles /local on

これを実行してPCを再起動する
ただし、システムのパフォーマンスが下がるので必要に応じてOnにする必要がある

実行

openfiles を引数無しで実行すると一覧が表示される

BlueSilverCatBlueSilverCat

アクセス権限

$acl = Get-Acl .
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone","FullControl","Allow")
$acl.SetAccessRule($accessRule)
$acl | Set-Acl .
BlueSilverCatBlueSilverCat

空フォルダを消す

function searchDirectory {
  Param(
    [Parameter(Mandatory)][string] $path
  )
  Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue $path | Where-Object { $_.PSIsContainer }
}

function _deleteEmptyDirectories {
  param (
    [Parameter(Mandatory)][array] $paths,
    [Alias("w")][switch] $whatif
  )
  foreach ($dir in $dirs) {
    if (-not ($dir.PSIsContainer)) {
      continue
    }
    if ( (Get-ChildItem -LiteralPath $dir | Measure-Object).Count -eq 0) {
      Write-Host "delete: " $dir
      Remove-Item $dir -WhatIf:$whatif
    }
  }
}

function deleteEmptyDirectories {
  param (
    [Parameter(Mandatory)][string] $path,
    [Alias("w")][switch] $whatif
  )
  $dirs = searchDirectory $path
  [array]::Reverse($dirs)
  _deleteEmptyDirectories $dirs -whatif:$whatif
}