Powershell suppress specific error message and not others



  • Going off of my earlier topic File Management removing unprintable characters I've got something that works.

    This is what works:

    (Get-ChildItem -Path "path\to\folder\" -Recurse | Rename-Item -NewName {$_.Name -replace '•',''} -verbose -ErrorAction SilentlyContinue -ErrorVariable daError)
    if ($daError) {
    	Write-Output "ERROR - There was an error. Pay Attention : [$daError]"
    	}
    

    For the final resulting Errors I get

    	ERROR - There was an error. Pay Attention : [Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Sourc
    e and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source a
    nd destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and 
    destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and des
    tination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destin
    ation path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destinati
    on path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination 
    path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination pat
    h must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path m
    ust be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must
     be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be
     different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be di
    fferent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be diffe
    rent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be differen
    t. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. 
    Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Sou
    rce and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source
     and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source an
    d destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and d
    estination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and dest
    ination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destina
    tion path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destinatio
    n path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination p
    ath must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path
     must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path mu
    st be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must 
    be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be 
    different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be dif
    ferent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be differ
    ent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different
    . Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. S
    ource and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Sour
    ce and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source 
    and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and
     destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and de
    stination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and desti
    nation path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destinat
    ion path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination
     path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination pa
    th must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path 
    must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path mus
    t be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must b
    e different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be d
    ifferent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be diff
    erent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be differe
    nt. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different.
     Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. So
    urce and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Sourc
    e and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source a
    nd destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and 
    destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and des
    tination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destin
    ation path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destinati
    on path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination 
    path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination pat
    h must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path m
    ust be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must
     be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be
     different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be di
    fferent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be diffe
    rent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be differen
    t. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. 
    Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Sou
    rce and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source
     and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source an
    d destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and d
    estination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and dest
    ination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destina
    tion path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destinatio
    n path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination p
    ath must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path
     must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path mu
    st be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must 
    be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be 
    different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be dif
    ferent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be differ
    ent. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different
    . Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. Source and destination path must be different. S
    ource and destination path must be different. Source and destination path must be different.]
    

    It would be ideal to suppress these actual non-errors (things that I don't care about since the files are fine where they are and I just want to see if some other Error is found.



  • @DustinB3403 said in Powershell suppress specific error message and not others:

    Going off of my earlier topic File Management removing unprintable characters I've got something that works.

    This is what works:

    (Get-ChildItem -Path "path\to\folder" -Recurse | Rename-Item -NewName {$_.Name -replace '•',''} -verbose -ErrorAction SilentlyContinue -ErrorVariable daError)
    if ($daError) {
    Write-Output "ERROR - There was an error. Pay Attention : [$daError]"
    }

    For the final resulting Errors I get...

    It would be ideal to suppress these actual non-errors (things that I don't care about since the files are fine where they are and I just want to see if some other Error is found.

    I can't reproduce the problem, but you can try changing the error handling like this:

    (Get-ChildItem -Path "path\to\folder" -Recurse | Rename-Item -NewName {$_.Name -replace '•',''} -verbose -ErrorAction SilentlyContinue -ErrorVariable daError)
    if ($daError.Exception.Message -notmatch "Source and destination path must be different") {
        Write-Output "ERROR - There was an error. Pay attention : [$daError]"
    }
    

    I don't know exactly the error path, maybe you can show me $daError.Exception | GM* if that doesn't work.



  • @Obsolesce said in Powershell suppress specific error message and not others:

    @DustinB3403 said in Powershell suppress specific error message and not others:

    Going off of my earlier topic File Management removing unprintable characters I've got something that works.

    This is what works:

    (Get-ChildItem -Path "path\to\folder\" -Recurse | Rename-Item -NewName {$_.Name -replace '•',''} -verbose -ErrorAction SilentlyContinue -ErrorVariable daError)
    if ($daError) {
      Write-Output "ERROR - There was an error. Pay Attention : [$daError]"
      }
    

    For the final resulting Errors I get...

    It would be ideal to suppress these actual non-errors (things that I don't care about since the files are fine where they are and I just want to see if some other Error is found.

    I can't reproduce the problem, but you can try changing the error handling like this:

    (Get-ChildItem -Path "path\to\folder" -Recurse | Rename-Item -NewName {$_.Name -replace '•',''} -verbose -ErrorAction SilentlyContinue -ErrorVariable daError)
    if ($daError.Exception.Message -notmatch "Source and destination path must be different") {
        Write-Output "ERROR - There was an error. Pay attention : [$daError]"
    }
    

    I don't know exactly the error path, maybe you can show me $daError.Exception | GM* if that doesn't work.

    That seems to work, the resulting output is a nice clean blank line. (since there are no errors that I care about)



  • Why not filter out the files that you don't need to rename?
    Summat like:

    (Get-ChildItem -Path "path\to\folder" -Recurse | 
            Where-Object {$_.Name -contains '•'} |
            Rename-Item -NewName {$_.Name -replace '•',''} -verbose -ErrorAction SilentlyContinue -ErrorVariable daError)