1

I want to create a function to remove a program through powershell. What I can't seem to figure out is how exactly to do that.

My code:

function RemoveProgram {
    $app = Get-CimInstance -Class Win32_Product | Where-Object {
        $_.Name -match $args
    }
    $app.Uninstall()
}

However, when I run this, I get

Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named 'Uninstall'.

Does anyone have some tips on how to make it better?

3
  • 3
    the CIM cmdlets do not have a live connection to the data. it is by design. you need to invoke the method with Invoke-CimMethod OR switch back to using Get-WmiObject since it still has live connections to the data. Commented May 8, 2020 at 23:58
  • Get-WmIObject doesn't work on my version of powershell Commented May 9, 2020 at 14:09
  • ah! then you likely otta put that info - and your PoSh version - in your Question. it is important ... [grin] Commented May 9, 2020 at 17:00

3 Answers 3

1

This code will work. It may take while to run though.

param(
    [string] $programToUninstall = "BlueJeans"    
)

function RemoveProgram([string] $program) {
    Invoke-CimMethod `
        -Query ('select * from Win32_Product where name like "%' + $program + '%"') `
        -MethodName "Uninstall" 
}

RemoveProgram -program $programToUninstall
Sign up to request clarification or add additional context in comments.

1 Comment

You can also pipe get-ciminstance to invoke-cimmethod.
1

Powershell 5 and msi installs only. Win32_Product is msi only as well, and verifies every msi when it runs, which is why it's so slow.

function RemoveProgram {
  get-package *$args* | uninstall-package
}

1 Comment

@thewindowsnoob Maybe later. Right now PS 7 doesn't support msi or programs providers.
0

So I was actually able to find a way to do this without creating my own function. I used a module called ProgramManagement. https://github.com/pldmgg/ProgramManagement

Hope this helps and thanks to anyone who tried to help me solve this!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.