Add -ErrorAction to a command

I have part of the script here:

$DotNET6_Installed = ((Get-Package -Name 'Microsoft .NET SDK 6*' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'Name').Replace('Microsoft .NET SDK ', '')).Replace(' (x64)', '')
$DotNET8_Installed = ((Get-Package -Name 'Microsoft .NET SDK 8*' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'Name').Replace('Microsoft .NET SDK ', '')).Replace(' (x64)', '')

I wish to add -ErrorAction SilentlyContinue so that I won’t get the following errors:

You cannot call a method on a null-valued expression.
At line:20 char:1
+ $DotNET6_Installed = ((Get-Package -Name 'Microsoft .NET SDK 6*' -Err ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:22 char:1
+ $DotNET8_Installed = ((Get-Package -Name 'Microsoft .NET SDK 8*' -Err ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

But I have no idea where to add the -ErrorAction to…
I just want to hide those errors.

It’s probably the replace method throwing the error. IMO, should split it up anyway or do a try catch.

just put all your code inside a try block, then leave the catch empty if you don’t want to do anything.

or you can do something like this:

$DotNET6_Installed = Get-Package -Name 'Microsoft .NET SDK 6*' -ErrorAction SilentlyContinue
if ($DotNet6_Installed) {
#Do your select object here with replace method, assign to same or another var
}

Thank you very much!