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
Please also mark the comments that contributed to the solution of the article
Content-ID: 656544
Url: https://administrator.de/contentid/656544
Printed on: December 3, 2024 at 23:12 o'clock
6 Comments
Latest comment
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
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.
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" ... 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
Hello Natalie,
to be sure, you can combine both methods from @147669. 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 @147669. 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
}
}