Powershell Uninstall-Package
Hello guys,
sorry for my bad english
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
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
Greetz,
Natalie
sorry for my bad english
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
Greetz,
Natalie
6 Antworten
- LÖSUNG 147669 schreibt am 26.02.2021 um 08:11:47 Uhr
- LÖSUNG natalie.solero schreibt am 26.02.2021 um 08:22:03 Uhr
- LÖSUNG 147669 schreibt am 26.02.2021 um 09:38:43 Uhr
- LÖSUNG natalie.solero schreibt am 26.02.2021 um 10:25:28 Uhr
- LÖSUNG 147669 schreibt am 26.02.2021 um 10:26:12 Uhr
- LÖSUNG natalie.solero schreibt am 26.02.2021 um 10:25:28 Uhr
- LÖSUNG 147669 schreibt am 26.02.2021 um 09:38:43 Uhr
- LÖSUNG natalie.solero schreibt am 26.02.2021 um 08:22:03 Uhr
- LÖSUNG chkdsk schreibt am 26.02.2021 um 13:05:04 Uhr
LÖSUNG 26.02.2021, aktualisiert um 09:37 Uhr
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
Have a closer look at the docs for detailed explanations:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell. ...
Best regards
SK
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}
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell. ...
Best regards
SK
LÖSUNG 26.02.2021, aktualisiert um 09:27 Uhr
thank you so much for your answer
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
it works for a couple of software but not for all.
Any Ideas?
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}
Any Ideas?
LÖSUNG 26.02.2021, aktualisiert um 09:57 Uhr
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.LÖSUNG 26.02.2021 um 10:25 Uhr
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
i can't see GIMP...so it is not passible to deinstall GIMP
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
LÖSUNG 26.02.2021, aktualisiert um 10:40 Uhr
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.
Welcome to super duper "Winblows Package Management"
... 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
Welcome to super duper "Winblows Package Management"
Wish you success. I'm out.
Regards
SK
LÖSUNG 26.02.2021 um 13:05 Uhr
Hello Natalie,
to be sure, you can combine both methods from @SchmitzKatz. Maybe he has another idea
But i'm not sure if it's getting all software or is able to uninstall, please test it yourself.
to be sure, you can combine both methods from @SchmitzKatz. Maybe he has another idea
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
}
}