0

i'm new at powershell scripts

i want to uninstall softwares located in remote computer from my local computer so i'm using

$computer =Read-Host -Prompt 'Input your server name or IP adress'
$user =Read-Host -Prompt 'Input your server username'
Invoke-Command -ComputerName $computer -ScriptBlock {  

Write-host -ForegroundColor Magenta "Please select the software you wish to uninstall..."


    $javaVer = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | select DisplayName, UninstallString, InstallLocation, InstallDate | out-gridview -PassThru



    write-host -ForegroundColor Yellow "The following software will be uninstalled:"




ForEach ($ver in $javaVer) {

    If ($ver.UninstallString) {

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

}
} -credential $user

testing it in my local machine i got this error

Out-GridView ne fonctionne pas à partir d’une session à distance. + CategoryInfo : InvalidOperation : (Microsoft.Power...GridViewCommand:OutGridViewCommand) [Out-GridView] , NotSupportedException + FullyQualifiedErrorId : RemotingNotSupported,Microsoft.PowerShell.Commands.OutGridViewCommand + PSComputerName : 192.168.1.200

google translated:

Out-GridView does not work from a remote session. + CategoryInfo: InvalidOperation: (Microsoft.Power ... GridViewCommand: OutGridViewCommand) [Out-GridView], NotSupportedException + FullyQualifiedErrorId: RemotingNotSupported, Microsoft.PowerShell.Commands.OutGridViewCommand + PSComputerName: 192.168.1.200

but if i send a simple command like ipconfig ,it work

What i'm doing wrong ?

8
  • 2
    the Out-GridView cmdlet requires that one be in an interactive session. the session that Invoke-Command uses is both on the other system AND non-interactive. if you need user interaction, split your remote calls into two parts & use the returned info from the 1st in your local Out-GridView display. Commented Oct 10, 2019 at 0:48
  • @Lee_Dailey sorry i'm still learning powershell scripts, can you modify the code and post it as an answer please? Commented Oct 10, 2019 at 0:52
  • are you wanting to just uninstall a specific app - java, perhaps? that is implied by your $JavaVer variable name ... Commented Oct 10, 2019 at 1:01
  • @Lee_Dailey no, i want to choose from the opened list the software that i want to uninstall or input the name of the software but choosing from the list is easier for me Commented Oct 10, 2019 at 1:06
  • OK ... i think the code in the ANSWER i just posted will work, but i cannot fully test is since i have only one system - and no apps that i want to uninstall. [grin] Commented Oct 10, 2019 at 1:25

1 Answer 1

1

since you CANNOT run Out-GridView in a remote, non-interactive session [what you get from Invoke-Command], this breaks the process into steps ...

  • asks for the target system name
  • sets up two I-C scriptblocks
  • gets the remote installed app list via I-C & the 1st scriptblock
  • shows that via O-GV for the user to choose apps
  • shows the to-be-uninstalled apps
  • does the actual remote uninstall via a 2nd call to I-C using the 2nd scriptblock

note that the actual command to uninstall the apps has been converted to an expanded string. remove the double quotes around "& cmd /c $uninst /quiet /norestart" when ready to really do it.

also, there is NO error handling at all. [grin]

# the following likely should include a test to see if the system actually exists
$ComputerName = Read-Host -Prompt 'Input your server name or IP adress '
#$user = Read-Host -Prompt 'Input your server username'
Write-Host ''

$ICScriptblock_One = {
    $HKLM_TargetList = @(
        "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"
        "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
        )
    # send the results to the calling system
    Get-ItemProperty -Path $HKLM_TargetList |
        Where-Object {
            # filter out items with blanks in the follwing properties
            $_.DisplayName -and
            $_.UninstallString
            } |
        Sort-Object -Property DisplayName |
        Select-Object -Property DisplayName, UninstallString, InstallLocation, InstallDate
    } # end >>> $ICScriptblock_One = {

$ICScriptblock_Two = {
    ForEach ($ATR_Item in $Using:AppsToRemove)
        {
        $uninst = $ATR_Item.UninstallString
        # remove the double quotes when ready to do this for real
        "& cmd /c $uninst /quiet /norestart"
        }
    } # end >>> $ICScriptblock_Two = {

$InstalledAppList = Invoke-Command -ComputerName $ComputerName -ScriptBlock $ICScriptblock_One

$OGVMessage = 'Please select the software you wish to uninstall...'
$AppsToRemove = $InstalledAppList |
    Out-GridView -Title $OGVMessage -OutputMode Multiple

write-host -ForegroundColor Yellow "The following software will be uninstalled :"
$AppsToRemove.DisplayName |
    Out-Host
Write-Host ''

Invoke-Command -ComputerName $ComputerName -ScriptBlock $ICScriptblock_Two

output on screen ...

Input your server name or IP adress : localhost

The following software will be uninstalled :
Apple Software Update
Google Update Helper
Python 3.7.4 Utility Scripts (64-bit)
Speccy

& cmd /c MsiExec.exe /I{A30EA700-5515-48F0-88B0-9E99DC356B88} /quiet /norestart
& cmd /c MsiExec.exe /I{60EC980A-BDA2-4CB6-A427-B07A5498B4CA} /quiet /norestart
& cmd /c MsiExec.exe /I{16F74529-EDE0-4BBD-B2AF-89AF9C696EA8} /quiet /norestart
& cmd /c "C:\Program Files\Speccy\uninst.exe" /quiet /norestart
Sign up to request clarification or add additional context in comments.

10 Comments

i'm testing on VLC media player, but didn't uninstalled, also i'm not getting the same output, this part not shown & cmd /c MsiExec.exe /I{A30EA700-5515-48F0-88B0-9E99DC356B88} /quiet /norestart & cmd /c MsiExec.exe /I{60EC980A-BDA2-4CB6-A427-B07A5498B4CA} /quiet /norestart & cmd /c MsiExec.exe /I{16F74529-EDE0-4BBD-B2AF-89AF9C696EA8} /quiet /norestart & cmd /c "C:\Program Files\Speccy\uninst.exe" /quiet /norestart
if you run the code as is ... no changes at all ... and give your local system as the $ComputerName, do the selected apps show up at all? the last two blocks of text should show the selected apps & the uninstall commands that would be run.
yes, without changes at all, it gives the same output of yours
the code i posted in my question uninstall msi and non-msi installer, so VLC for example uninstalled successfully, can you look at your code and the code i posted maybe it need some changes in order to uninstall all the softwares,
cmd /c "C:\Program Files (x86)\VideoLAN\VLC\uninstall.exe" /quiet /norestart when i write it in cmd it work, not silently, but it shows the window to do the uninstall process
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.