0

I am trying expand my existant Script with better code. So i wanted to let the script proof if the software really is uninstalled or isn't. I tried to do that with foreach. Here's my code:

function deletesoftware2 () {
$ErrorActionPreference = 'SilentlyContinue'
$appname = @(
    #"*HP Support Assistant*",
    #"*HP Support Solutions Framework*",
    #"*Dell SupportAssist*",
    #"*Dell Command | Update*",
    "*NuGet*"
)
ForEach($app in $appname){
    Write-Host "Removing $app from System"
    Get-Package -Name $app | Uninstall-Package -ErrorAction SilentlyContinue | Out-Null
        Start-Sleep -Seconds 20
    Get-Package | Where-Object{$_.Name -like $app} | Format-Table name | Out-String -OutVariable software | Out-Null
    if ($software -match $app) {
        Write-Host "$app wasnt found or could not removed"

    }
    else {
        Write-Host "$app was removed"   
    }        
}
Start-Sleep -Seconds 10

}

Edit: If i run the script that is shown, it jumps directly into the else, even though the software isn't removed. I tried to replace the "$app" in the if directly to NuGet and it worked. But if i write host the $app variable, it says exactly Nuget, and that's what i want. The prolem is, that it still won't work.

Well that doesn't work how i want it to, i'd be glad if someone can help me! Thanks in advance!

2
  • Please edit the question and explain what's the actual outcome. No packages are being removed? Wrong packages are being removed? Something else? Commented Apr 14, 2020 at 10:35
  • @vonPryz i edited it, sorry for my englisch, i hope you can understand, what i meant. Commented Apr 14, 2020 at 10:59

1 Answer 1

1

The reason for this behavior is that Format-Table name | Out-String -OutVariable software will provide a bit unexpected results.

Let's use Microsoft Teams package as an example as it's available on my system.

$q = Get-Package | ? { $_.name -eq "Microsoft Teams" }
$q.Name
Microsoft Teams

$q.Name.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

So far, everything looks good. Now let's bring Format-Table into the game like so,

$q | Format-Table name | Out-String -OutVariable software | Out-Null
$software

Name
----
Microsoft Teams

$software.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ArrayList                                System.Object

Wait, what? The type is not same at all. That's because formatting as a table will create an array of strings. That will never match the simple string name of a package.

As for a fix, just use Get-Package again and assign its result in a variable. If there isn't such a package, the output will be equal to $null. Like so,

Get-Package -Name $app | Uninstall-Package...
$q= Get-Package | Where-Object{$_.Name -like $app}
if($q -eq $null) {
    Write-Host "$app was removed"
}
else {
    Write-Host "$q is still present"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thats brilliant, thanks for the help. By the way I really had to smile when I read your text ^^ . I really appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.