user2995217
user2995217

Reputation: 1

Uninstalling software on a remote client using powershell

I'm looking for a script that will help me uninstall a certain software on several clients in my network.

Right now i'm going through a list, access the client remotely, sign in with my administrator account and uninstalling the software before logging out and repeating the process. All of this is manually so i would like your help to write a powershell script that does these things for me.

Some Problems that might occur: I can't log in remotely because i can't establish a connection to the client. Another user might already be logged in on the client. The software to be uninstalled is actually already uninstalled without my knowledge.

It's somewhere around 900 clients so a script would really help out.

Also, if it would be possible to, after the script is finished, to get a list of which clients that the software was uninstalled on and which clients it weren't would be great.

Upvotes: 0

Views: 4865

Answers (1)

David Martin
David Martin

Reputation: 12248

Questions written like this are likely to elicit 'What have you tried' type responses...

I would recommend using the Windows Installer Powershell Module Uninstall-MSIProduct.

I've described how to use this module remotely in this post: remote PCs using get-msiproductinfo, this example uses Get-MSIProductInfo but could be easily updated to use Uninstall-MSIProduct.

I've had a quick go at changing this to use Uninstall-MSIProduct, but haven't tested it.

[cmdletbinding()]
param
(
    [parameter(Mandatory=$true,ValueFromPipeLine=$true,ValueFromPipelineByPropertyName=$true)]
    [string]
    $computerName,
    [string]
    $productCode
)

begin
{
    write-verbose "Starting: $($MyInvocation.MyCommand)"

    $scriptFolder   = Split-Path -Parent $MyInvocation.MyCommand.Path
    $moduleName     = "MSI"
    $modulePath     = Join-Path -Path $scriptFolder -ChildPath $moduleName  

    $remoteScript   = {
        param($targetPath,$productCode)

        Import-Module $targetPath
        uninstall-msiproduct -ProductCode $productCode
    }

    $delayedDelete  = {
        param($path)
        Remove-Item -Path $path -Force -Recurse
    }
}
process
{
    $remotePath = "\\$computerName\c$\temp\$moduleName"

    write-verbose "Copying module to $remotePath"
    Copy-Item -Path $modulePath -Destination $remotePath -Recurse -Container -Force

    write-verbose "Getting installed products"
    Invoke-Command -ComputerName $computerName -ScriptBlock $remoteScript -ArgumentList "c:\temp\$moduleName", $productCode

    write-verbose "Starting job to delete $remotePath"
    Start-Job -ScriptBlock $delayedDelete -ArgumentList $remotePath | Out-Null
}

end
{
    write-verbose "Complete: $($MyInvocation.MyCommand)"
}

Upvotes: 1

Related Questions