Help us understand the problem. What is going on with this article?

Remove-Item に -Verbose を指定すると ErrorActionPreference が無効になる

More than 1 year has passed since last update.
テストコード
$ErrorActionPreference = "Stop"

# 存在しないファイル
$file = "aaa.log"

try {
    "Remove-Item $file" | Out-Host
    Remove-Item $file
} catch {
    "エラー発生1" | Out-Host
}

try {
    "Remove-Item $file -Verbose" | Out-Host
    Remove-Item $file -Verbose
} catch {
    "エラー発生2" | Out-Host
}

try {
    "Remove-Item $file -Verbose -ErrorAction Stop" | Out-Host
    Remove-Item $file -Verbose -ErrorAction Stop
} catch {
    "エラー発生3" | Out-Host
}

"処理終了" | Out-Host
実行結果
Remove-Item aaa.log
エラー発生1
Remove-Item aaa.log -Verbose
Remove-Item : パス 'C:¥aaa.log' が存在しないため検出できません。
発生場所 C:¥Users¥nonaka¥Desktop¥remove-item_error.ps1:15 文字:5
+     Remove-Item $file -Verbose
+     ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
    + CategoryInfo          : ObjectNotFound: (C:¥aaa.log:String) [Remove-Item 
   ], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov 
   eItemCommand

Remove-Item aaa.log -Verbose -ErrorAction Stop
エラー発生3
処理終了

Remove-Item-Verboseを指定すると、$ErrorActionPreferenceStopを指定しているのに、2番目のように例外がキャッチできなくなる。3番目のように-ErrorAction Stopを個別に指定すると例外がキャッチできる。

Copy-Item-Verbose指定しても、この現象は起きない。改めて確認したところ、Copy-Itemでも同じ現象が発生した。特定のコマンドレットによらない模様。(詳細はコメント欄参照。)

flasksrw
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした