natalie.solero
Goto Top

Powershell Uninstall-Package

Hello guys,

sorry for my bad english face-smile


i'm trying to create a powershell script to uninstall software on a remote machine.
i enter the machine name, get the installed software as enumeration and prompt the number for the specific software.

This is what i have

$pcname = read-host "give hostname"  
$SofNames = Invoke-Command -ComputerName $pcname -ScriptBlock {
    Get-Package -ProviderName programs -IncludeWindowsInstaller | Select-Object -ExpandProperty Name | sort-object}

for($i=0; $i -lt $SofNames.count;$i++){
    "$($i): $($SofNames[$i])"}  

$iN = Read-Host -Prompt "enter number"  
$iNi = $SofNames[$iN]

Invoke-Command -ComputerName $pcname -ScriptBlock { Uninstall-Package -Name "$iNi" }  

Unfortunately, it doesn't work for me. In the Uninstall-function i get the following failure:
"The argument cannot be bound to the name parameter because it is an empty string."

I hope you can help me face-smile

Greetz,
Natalie

Content-ID: 656544

Url: https://administrator.de/contentid/656544

Printed on: December 3, 2024 at 23:12 o'clock

147669
Solution 147669 Feb 26, 2021, updated at May 16, 2023 at 13:17:16 (UTC)
Goto Top
Hi Natalie,
your problem is that you are defining a local Variable $iNi which does not exist on the remote side when you call the command via Invoke-Command.
To use this variable on the remote side you can either pass it as a parameter to Invoke-Command by using the -Argumentlist parameter or use the special Variable-Prefix $using: in the remote scriptblock to access local session variables
Invoke-Command -ComputerName $pcname -ScriptBlock { Get-Package -Name  $using:iNi | Uninstall-Package -Force}
Have a closer look at the docs for detailed explanations:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell. ...

Best regards
SK
natalie.solero
natalie.solero Feb 26, 2021, updated at May 16, 2023 at 13:17:16 (UTC)
Goto Top
thank you so much for your answer face-smile
i just tried your solution but it also doesn't work for me. Now it says it can't find a package with this name. I't doesn't matter which software i choose. It is strange because at first it shows me all installed software.

I have figured out, that when i am using
Invoke-Command -ComputerName $pcname -ScriptBlock {get-Package -Name $Using:iNi | Uninstall-Package}  
it works for a couple of software but not for all.
Any Ideas?
147669
147669 Feb 26, 2021, updated at May 16, 2023 at 13:17:16 (UTC)
Goto Top
it works for a couple of software but not for all.
Not every software can be uninstalled with this cmdlet only specific ones. Here you have to to it via CIM/WMI to uninstall.
natalie.solero
natalie.solero Feb 26, 2021, updated at May 16, 2023 at 13:17:16 (UTC)
Goto Top
That is really crazy...
If i use the get-package command i can see all installed software on the remote machine, for example GIMP but i can't uninstall it.
If i try it with
$PCName = Read-Host "give hostname"				  
Get-WmiObject Win32_Product -ComputerName $PCName | Select-Object -Property Name
i can't see GIMP...so it is not passible to deinstall GIMP
147669
Solution 147669 Feb 26, 2021, updated at May 16, 2023 at 13:17:16 (UTC)
Goto Top
No, thats totally normal in MS environment! The reason is, if a software installer has not registered with windows installer it does not appear in this list. So you have to grab the registry key UninstallString key for this software and invoke the uininstall command with silent switches.

screenshot

Welcome to super duper "Winblows Package Management" face-wink ... If i were you i would switch to choclately or another software package manager like WSUS PP and so on.

Wish you success. I'm out.

Regards
SK
chkdsk
Solution chkdsk Feb 26, 2021, updated at May 16, 2023 at 13:17:16 (UTC)
Goto Top
Hello Natalie,
to be sure, you can combine both methods from @147669. Maybe he has another idea face-smile
But i'm not sure if it's getting all software or is able to uninstall, please test it yourself.
$pcname = read-host "give hostname"  
$SofNames = Invoke-Command -ComputerName $pcname -ScriptBlock {
    Get-Package -ProviderName programs -IncludeWindowsInstaller  | ?{$_.Name -notlike "Update for*" -and $_.Name -notlike "Security Update for*"} | Select-Object -ExpandProperty Name | sort-object}  

for($i=0; $i -lt $SofNames.count;$i++){
    "$($i): $($SofNames[$i])"}  

$iN = Read-Host -Prompt "give number"  
$SofNames[$iN] 
$iNi = $SofNames[$iN]


$result = Invoke-Command -ComputerName $pcname -ScriptBlock {get-Package -Name $Using:iNi | Uninstall-Package}  
write-host "uninstalling. please wait"  

if ($result -ne $null)
    { 
 write-host "success" -foregroundcolor green  
 
 }
 else
 { 
 write-host "failed" -foregroundcolor red  
 write-host "try second method" -foreground yellow  
$uninstall = Invoke-Command -ComputerName $pcname -ScriptBlock {
$javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match "$Using:ini"} |  
            Select-Object -Property DisplayName, UninstallString

ForEach ($ver in $javaVer) {

    If ($ver.UninstallString) {

        $uninst = $ver.UninstallString
        & cmd /c $uninst /verysilent /norestart
    }

}

}
if ($uninstall -ne $null)
{
write-host "failed" -foregroundcolor red  
}
else 
{
write-host "success" -foregroundcolor green  
}
}