Share via


Get-Item - ItemNotFoundException is not caught

Question

Friday, June 3, 2011 8:06 AM

In my powershell script, I am facing one problem.

The code is something like this -

try
     {
            [string] $destFileName = $global:logFilePath + ".bak" 
            $IfDestExists = Get-Item $destFileName
            if($IfDestExists)
           {
                $remItem = Remove-Item $destFileName -Force
            }
            $renPath = Rename-Item $global:logFilePath $destFileName                           
           $nuPath = New-Item $global:logFilePath -type file       
     }
     catch
     {
             #Exception Handling
          
     }     

The problem is , ItemNotFoundException is not caught in "try-catch" block.

All replies (2)

Friday, June 3, 2011 8:11 AM âś…Answered | 1 vote

add to Get,Remove,New-Item -erroraction stop

Get-Item $destFileName -erroraction stop


Saturday, June 4, 2011 2:42 AM | 1 vote

try is for *terminating* errors only.  Kazun has provided you a way to "change your non-terminating error into a terminating one".