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!