Search code examples
windowspowershellpowershell-2.0

Need to install Microsoft Visual C++ 2015-2022 via Powershell


I have to install the below Visual C++ software via powershell.

  • VC_redist_2015-2022x64
  • VC_redist_2015-2022x86

I have to first check whether the Visual C++ x64 & x86 installed on my server. If its installed already code have to skip the installation part or else it have to install the Visual C++ x64 & x84. I have a powershell code which is not working properly. I am trying to short via name. But my code is not working.

Could someone please help me to fix it

I am looking forward to hearing from you.

$MSRedistributable = Get-WmiObject -Query "Select * from Win32_Product where name = 'Microsoft Visual C++'"
$MSRedistributableInstaller = "C:\Software"

Write-host "`nStaring Installation of Microsoft Visual C++ 2015-2022"
if (!($MSRedistributable.name))
{
Write-host "Installing Microsoft Visual C++."
Start-Process -Filepath "$($MSRedistributableInstaller)\VC_redist_2015-2022x64" -Argumentlist "/Q" -wait
Start-Process -Filepath "$($MSRedistributableInstaller)\VC_redist_2015-2022x86" -Argumentlist "/Q" -wait
write-host "Microsoft Visual C++ installed successfully."
}
else
{
write-host "$($MSRedistributable.name) appears to be installed already.  Skipping."
}

Solution

  • I have a guess about your question. In your code, on line 1, the query statement should be modified from = to like, similar to the example:

    (base) PS C:\Users\tzcbz> Get-WmiObject -Query "Select * from Win32_Product where name = 'Microsoft Visual C++'"
    
    (base) PS C:\Users\tzcbz> Get-WmiObject -Query "Select * from Win32_Product where name like 'Microsoft Visual C++'"
    
    (base) PS C:\Users\tzcbz> Get-WmiObject -Query "Select * from Win32_Product where name like 'Microsoft Visual C++%'"
    
    
    IdentifyingNumber : {1D8E6291-B0D5-35EC-8441-6616F567A0F7}
    Name              : Microsoft Visual C++ 2010  x64 Redistributable - 10.0.40219
    Vendor            : Microsoft Corporation
    Version           : 10.0.40219
    Caption           : Microsoft Visual C++ 2010  x64 Redistributable - 10.0.40219
    

    Update: In response to your further questions

    Hi,

    In response to your concern, I've successfully tested the updated code on my system which runs Windows 11 Insider and uses PowerShell version 5.1.22621.2706. The code performs as expected and yields the correct results.

    However, when I ran it on an older computer equipped with Windows 7 and a PowerShell version dating back to before 2009, it encountered errors resulting in unpredictable behavior. The issue stems from the fact that the conditional check in the if-statement does not function correctly on earlier PowerShell versions.

    To address this, I've made an amendment to line 5 of the code. Following this modification, the updated script should operate smoothly across all platforms, including the older Powershell versions.

    $MSRedistributable = Get-WmiObject -Query "Select * from Win32_Product where name like 'Microsoft Visual C++%'"
    $MSRedistributableInstaller = "C:\Software"
    
    Write-host "`nStaring Installation of Microsoft Visual C++ 2015-2022"
    if (($null -eq $MSRedistributable) -or (!$MSRedistributable.PSObject.Properties.Match("name")))
    {
    Write-host "Installing Microsoft Visual C++."
    Start-Process -Filepath "$($MSRedistributableInstaller)\VC_redist_2015-2022x64" -Argumentlist "/Q" -wait
    Start-Process -Filepath "$($MSRedistributableInstaller)\VC_redist_2015-2022x86" -Argumentlist "/Q" -wait
    write-host "Microsoft Visual C++ installed successfully."
    }
    else
    {
    write-host "$($MSRedistributable.name) appears to be installed already.  Skipping."
    }