Application Uninstall Script

Hello,

I am fairly new to PowerShell and I started working on a script to uninstall application from machines. I was watching the Getting Started with PowerShell course on MVA and I saw the cmdletbinding which can be used to prompt users for parameters and I thought I could use this fairly easy. So I started messing with a small script and now it has become larger than anything I ever created since I decided to add conditional statements.

Basically what I would like to do is there are multiple computer and applications, if Y the parameter become strings. Afterwards list all the application installed on the machine/s (and add machine name to the list). I am guessing that I would need to add-member the computer name. The reason behind this list is to have the exact name for the application that would be uninstalled if there are better ways to go about this I am all for it.

So the problem that I having with my script is that after the first user the next parameter is not prompted and then script errors. Any suggestions? and if you have time explanations so I can better understand why my script is not working.

Thanks in advance.

I have made some changes to the script. I am not sure which would be the best way to proceed.

This is the old script.

$multicomp= Read-Host "Do you want to target multiple computers? Y/N"

#For multiple computers
if ($multicomp -match 'Y' -or 'y')
{
    {[CmdletBinding()]
          Param(
         #Enter the computer name.
            [Parameter(Mandatory=$True)]
            [string[]]$computer
            )
    }
 }

 #For single computer
 elseif ($multicomp -match 'N' -or 'n')
 {
    {[CmdletBinding()]
          Param(
         #Enter the computer name.
            [Parameter(Mandatory=$True)]
            $computer
             )
    }
 }

 else
 {
    Write-Host "Invalid Input. Please Try Again"
 }

Get-WmiObject Win32_Product -ComputerName $computer | Add-Member -NotePropertyName ComputerName -NotePropertyValue $computer | Select Name, Version, Caption | Sort-Object Name | Out-GridView

Write-Host " Do you want to uninstall multiple applications? Y/N"
$multiapp= Read-Host

#for multiple applications
if ($multiapp -match 'Y' -or 'y')
{
    {[CmdletBinding()]
          Param(
          #Enter the application name. Name must be exact.
            [Parameter(Mandatory=$True)]
            [string[]]$appname
            )
    }
}

#for single application
elseif ($multiapp -match 'N' -or 'n')
{
    {[CmdletBinding()]
          Param(
          #Enter the application name. Name must be exact.
            [Parameter(Mandatory=$True)]
            $appname
            )
    }
}

else
{
    Write-Host "Invalid Input. Please Try Again"
}

#Uninstall applicaiton based on the name value
$app= Get-WmiObject Win32_Product -ComputerName $computer | where { $_.name -eq $appname }
$app.Uninstall()

#Display success message.
$message= "$appname has been succesfully uninstalled from $computer. Press any key to continue..."
Write-Host $message

#Wait for Key input to close.
$host.UI.RawUI.ReadKey()

New script

$multicomp= Read-Host "Do you want to target multiple computers? Y/N" 

#For multiple computers
If ($multicomp -eq "Y" -or "y")
{
    $computer = @()
    do {
        $input = (Read-Host "Enter Computer Names")
        if ($input -ne '')
            {
                $computer += $input
            }
       }
    until ($input -eq'')
 }

 #For single computer
 ElseIf ($multicomp -eq "N" -or "n")
 {
    $computer = Read-Host "Enter Computer Name"
 }

 Else
 {
    Write-Host "Invalid Input. Please Try Again"
 }

Get-WmiObject Win32_Product -ComputerName $computer | Select Name, Version, Caption | Sort-Object Name | Out-GridView

$multiapp= Read-Host " Do you want to uninstall multiple applications? Y/N"


#for multiple applications
If ($multiapp -eq 'Y' -or 'y')
{
    $appname = @()
    do {
        $input = (Read-Host "**Application names must be exact**  Enter Application Names")
        if ($input -ne '')
            {
                $appname += $input
            }
       }
    until ($input -eq'')
}

#for single application
ElseIf ($multiapp -eq 'N' -or 'n')
{
    $appname = Read-Host "**Application name must be exact** Enter Application Name"
}

Else
{
    Write-Host "Invalid Input. Please Try Again"
}

#Uninstall applicaiton based on the name value
$app= Get-WmiObject Win32_Product -ComputerName $computer | where { $_.name -eq $appname }
$app.Uninstall()

#Display success message.
$message= "$appname has been succesfully uninstalled from $computer. Press any key to continue..."
Write-Host $message

#Wait for Key input to close.
$host.UI.RawUI.ReadKey()

Is there any reason you are not using an advanced function?
I think you would have a much easier and, more importantly, a more portable solution if you created this script in the style of a function.

I am not sure what you mean…

Or Honestly I have no idea what you are talking about. I am a noob.

I have modified my code. Currently it works but there are areas for improvement.

do
{    
    $multicomp= Read-Host "Do you want to target multiple computers? Y/N" 

    #For multiple computers
    If ($multicomp -eq "Y" -Or $multicomp -eq "y")
    {
         Write-Host "Press then 'Enter' key twice to end input" -ForegroundColor Yellow
         
         #Loop through user inputs and append to user variable
         $computer = @()
            do {
             $input = (Read-Host "Enter Computer Names")
                 if ($input -ne '')
                 {
                     $computer += $input
                 }
               }
            until ($input -eq'')
    }

    #For single computer
     ElseIf ($multicomp -eq "N" -Or $multicomp -eq "n")
    {
        $computer = Read-Host "Enter Computer Name"
    }

    Else
    {
        Write-Host "Invalid Input. Please Try Again"
    }

}
Until($multicomp -eq "Y" -Or $multicomp -eq "y" -Or $multicomp -eq "N" -Or $multicomp -eq "n")

Write-Host "Retrieving Application List..." -ForegroundColor Cyan

#Get application List for each of the computer that was entered
Get-WmiObject Win32_Product -ComputerName $computer | Select Name, Version | Sort-Object Name | Out-GridView -Title "Application List"

do
{    
    $multiapp= Read-Host "Do you want to uninstall multiple applications? Y/N"


    #for multiple applications
    If ($multiapp -eq "Y" -Or $multiapp -eq "y")
    {
        Write-Host "Press then 'Enter' key twice to end input" -ForegroundColor Yellow
        
        #Loop through user inputs and uninstalls application
        do {
            $appname = (Read-Host "**Application names must be exact**  Enter Application Names")
            if ($appname -ne '')
                 {
                 
                 Write-Host "Uninstalling application..." -ForegroundColor Cyan
                 
                 #Uninstall application based on the name value
                 $app= Get-WmiObject Win32_Product -ComputerName $computer | where { $_.name -eq $appname }
                 $app.Uninstall()
                 
                 Write-Host "$appname Succesfully Uninstalled" -ForegroundColor Green
                 }
           }
        until ($appname -eq'')
    }

    #for single application
    ElseIf ($multiapp -eq "N" -Or $multiapp -eq "n")
    {
        $appname = Read-Host "**Application name must be exact** Enter Application Name"
        
        Write-Host "Uninstalling application..." -ForegroundColor Cyan
        #Uninstall application based on the name value
        $app= Get-WmiObject Win32_Product -ComputerName $computer | where { $_.name -eq $appname }
        $app.Uninstall()

        Write-Host "$appname Succesfully Uninstalled" -ForegroundColor Green
    }

    Else
    {
        Write-Host "Invalid Input. Please Try Again"
    }
}
Until ($multiapp -eq "Y" -Or $multiapp -eq "y" -Or $multiapp -eq "N" -Or $multiapp -eq "n")


#Display success message.
$message= "The application/s have been succesfully uninstalled. Press any key to continue..."
Write-Host $message -ForegroundColor Green

#Wait for Key input to close.
$host.UI.RawUI.ReadKey()

Hi Michael,

i tested your script
it worked if i login on both computers with local admin rights, then i can delete application,
but if i run this script against user login - script finish but application not deleted