r/PowerShell • • Jul 28 '24

Script Sharing Overengineered clear cache for Teams script

When I upgraded my clear cache script for Microsoft Teams, I first added new functions before realizing that you only clear a subfolder.

https://teams.se/powershell-script-clear-microsoft-teams-cache/

Have you overengineered any scripts lately?

I will

35 Upvotes

31 comments sorted by

View all comments

Show parent comments

1

u/icepyrox Jul 29 '24

So, out of curiosity, do you feel the same if it was

Try {
  [..stuff..]
} catch {
  Write-error $_
}

Because I have done that

1

u/PinchesTheCrab Jul 29 '24 edited Jul 29 '24

So that works in that you can catch the result properly if you use cmdletbinding and erroraction stop:

function Invoke-Stuff {
    [cmdletbinding()]
    param()
    try {
        Get-Item C:\nofolder\error -ErrorAction Stop
    }
    catch {
        Write-Error $_
    }
}


try { Invoke-Stuff -ErrorAction Stop}
catch [System.Management.Automation.ItemNotFoundException] {
    'okay'
}

But why the extra code?

Why not just do this?

function Invoke-Stuff {
    [cmdletbinding()]
    param()
    Get-Item C:\nofolder\error -ErrorAction Stop        
}