locked
PowerShell Command Uninstall Apps RRS feed

  • General discussion

  • I require the ability to uninstall applications from multiple machines "silently" in the background using PowerShell commands, but do not know how to use Powershell very well yet.
    Wednesday, January 3, 2018 3:15 PM

All replies

  • Step 1: Retrieve the list of installed software from the remote computer:

    #------------- START Powershell Code-------------------------


    param (
        [Parameter(Mandatory,HelpMessage="Enter Remote Workstation (Name/DNS/IP):")] $RemoteComputer = "."
    )

    CLS

    Try
    {
        #Test Connection
        If(Test-Connection $RemoteComputer -Quiet -count 1) {
            Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer is Online." -BackgroundColor Green -ForegroundColor White
       
            $protocol = "DCOM"
            $cimsess = New-CimSession -ComputerName $RemoteComputer -OperationTimeoutSec 30 -SessionOption (New-CimSessionOption -Protocol $protocol) #-SkipTestConnection
            If($cimsess) {
                $OktoContinue = $true
            } Else {
                Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer ERROR - Unable to create WMI/CIM Session" -BackgroundColor Red -ForegroundColor White
                Throw "Unable to create WMI/CIM Session"
            }
        }
    } Catch {
        $OktoContinue = $false
    }

    If($OktoContinue -eq $true){
        Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer - Getting Installed Apps (timeout 180 sec)..."
        $InstalledApps = Get-CimInstance -CimSession $cimsess -Namespace "root\cimv2" -Class "Win32_Product" -OperationTimeoutSec 180 | Select IdentifyingNumber, Vendor, Name, Version
        $InstalledApps
    }

    Wednesday, January 3, 2018 3:34 PM
  • Step 2: create a process to execute the Uninstall command

    #-------------PowerShell Starts here -----------------------


    param (
        [Parameter(Mandatory,HelpMessage="Enter Remote Workstation (Name/DNS/IP):")] $RemoteComputer = ".",     
        [Parameter(Mandatory,HelpMessage="Enter The ProductID of the Software To be removed:")] $ProductID = ""
    )


    Function CreateRemoteProcess
    (
        [Parameter(Mandatory=$true)] [string]$Computer = $(throw "`nMissing -Computer"),
        [Parameter(Mandatory=$true)] [string]$CommandString = $(throw "`nMissing -CommandString")
    )
    {
        $Class = "Win32_Process"
        $Method = "Create"

        $MC = [WmiClass]"\\$Computer\ROOT\cimv2:$Class"


        $InParams = $mc.psbase.GetMethodParameters($Method)


        $InParams.CommandLine = $CommandString
        $InParams.CurrentDirectory = "C:\Windows\system32"
        $inparams.PSBase.properties | select name,Value | format-Table

        $R = $mc.PSBase.InvokeMethod($Method, $inParams, $Null)
        $R | Select ProcessID, ReturnValue | Format-Table

    }

    CLS

    Try
    {
        #Test Connection
        If(Test-Connection $RemoteComputer -Quiet -count 1) {
            Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer is Online." -BackgroundColor Green -ForegroundColor White
       
            $protocol = "DCOM"
            $cimsess = New-CimSession -ComputerName $RemoteComputer -OperationTimeoutSec 30 -SessionOption (New-CimSessionOption -Protocol $protocol) #-SkipTestConnection
            If($cimsess) {
                $OktoContinue = $true
            } Else {
                Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer `tERROR - Unable to create WMI/CIM Session" -BackgroundColor Red -ForegroundColor White
                Throw "Unable to create WMI/CIM Session"
            }
        }
    } Catch {
        $OktoContinue = $false
    }

    If($OktoContinue -eq $true){
        Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer `tLocating '$ProductID' (timeout 180 sec)..."
        $InstalledApps = Get-CimInstance -CimSession $cimsess -Namespace "root\cimv2" -Class "Win32_Product" -OperationTimeoutSec 180 -Filter "IdentifyingNumber = '$ProductID'" | Select IdentifyingNumber, Vendor, Name, Version
        If($InstalledApps){

            Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer `tFound: $($InstalledApps.Vendor) $($InstalledApps.Name) $($InstalledApps.Version)"

            $MyCommand = "msiexec /x ""$ProductID"" /qn /l* C:\windows\$($ProductID)_Uninstall.log"
       
            Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer `tExecuting: $MyCommand"

            CreateRemoteProcess -Computer $RemoteComputer -CommandString $MyCommand
        } Else {
            Write-Host "[$(Get-Date -Format yyyyMMdd-HH:mm:ss)] `t$RemoteComputer `tUnable to Locate '$ProductID'"
        }
    }

    Wednesday, January 3, 2018 4:55 PM