title: How to run / execute powershell commands in excel vba
body: <p>I’m writing some code to compare installed versions of software in some test computers currently I’m using a PowerShell PS1 script to create a text file and compare with previously created baseline text file</p>
  
  <p>For the ease of end users I would like to automate these in a excel file, press a button and you get a result of what does not match with the baseline</p>
  
  <p>My current ps1 scripts are</p>
  
  <blockquote>
    <p>:CreateDefaultApps.ps1 - Create the Default baseline DefApp32.txt or DefApp64.txt in My Documents folder</p>
  </blockquote>
  
  <pre><code>    cls
  $CurDir = $myinvocation.InvocationName | split-path -parent
  $DOCDIR = [Environment]::GetFolderPath("MyDocuments")
  $TARGETDIR = "$DOCDIR\AppLog"
  $COMPNAME = $env:computername
  if(!(Test-Path -Path $TARGETDIR ))
  {
      New-Item -ItemType directory -Path $TARGETDIR
  }
  cls
  if ([Environment]::Is64BitOperatingSystem)
  {
      Write-Host "64bit Windows Detected"
      Write-Host "Collecting Product Information for"$COMPNAME
      If (Test-Path $TARGETDIR\DefApp64.txt)
      {
          Remove-Item $TARGETDIR\DefApp64.txt
      }
      Else
      {
          Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version &gt; $TARGETDIR\DefApp64.txt
      }
      write-host "File"$TARGETDIR"\DefApp64.txt Created"
  }
  else
  {
      Write-Host "32bit Windows Detected"
      Write-Host "Collecting Product Information for"$COMPNAME
      If (Test-Path $TARGETDIR\DefApp32.txt)
      {
          Remove-Item $TARGETDIR\DefApp32.txt
      }
      Else
      {
          Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version &gt; $TARGETDIR\DefApp32.txt
      }
      write-host "File " $TARGETDIR"\DefApp32.txt Created"
  }
  </code></pre>
  
  <blockquote>
    <p>:CompareApps.ps1 creates current list of applications and compare with Baseline</p>
  </blockquote>
  
  <pre><code>cls
  $CurDir = $myinvocation.InvocationName | split-path -parent
  Function Abort
  {
  Write-Host "Script Aborted"
  }
  
  $DOCDIR = [Environment]::GetFolderPath("MyDocuments")
  $TARGETDIR = "$DOCDIR\AppLog"
  $COMPNAME = $env:computername
  if(!(Test-Path -Path $TARGETDIR ))
  {
      Write-Host $TARGETDIR" Folder does not Exist"
      Abort
  }
  
  Function Finish
  {
  write-host "Comparing Products Completed"
  }
  
  Function CreateCompNameFile
  {
  Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version &gt; $TARGETDIR\$COMPNAME.txt
  write-host "File"$TARGETDIR\$COMPNAME".txt Created"
  }
  
  Function Compare64
  {
  write-host "Comparing Products with file"$TARGETDIR\$COMPNAME".txt"
  Compare-Object -ReferenceObject (Get-Content $TARGETDIR\DefApp64.txt) -DifferenceObject (Get-Content $TARGETDIR\$COMPNAME.txt)
  Finish
  }
  
  Function Compare32
  {
  write-host "Comparing Products with file"$TARGETDIR\$COMPNAME".txt"
  Compare-Object -ReferenceObject (Get-Content $TARGETDIR\DefApp32.txt) -DifferenceObject (Get-Content $TARGETDIR\$COMPNAME.txt)
  Finish
  }
  
  
  cls
  if ([environment]::Is64BitOperatingSystem)
  {
      Write-Host "64bit Windows Detected"
      Write-Host "Collecting Product Information for"$COMPNAME
      If (Test-Path $TARGETDIR\$COMPNAME.txt)
      {
          Remove-Item $TARGETDIR\$COMPNAME.txt
          CreateCompNameFile
      }
      else
      {
          CreateCompNameFile
      }
      Compare64
  }
  else
  {
      Write-Host "32bit Windows Detected"
      Write-Host "Collecting Product Information for"$COMPNAME
      If (Test-Path $TARGETDIR\$COMPNAME.txt)
      {
          Remove-Item $TARGETDIR\$COMPNAME.txt
          CreateCompNameFile
      }
      else
      {
          CreateCompNameFile
      }
      Compare32
  }
  </code></pre>
  
  <p>I would prefer to run this code via excel and capture the list in a worksheet, then compare the baseline which is already in the worksheet. How do I execute or use commands from PowerShell in VBA? </p>
  
  <p>Previously I have used (see post <a href="https://stackoverflow.com/questions/27097726/how-to-list-all-installed-applications-in-to-excel">How to list all installed applications in to excel</a>)</p>
  
  <pre><code>Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; StrComputer &amp; "\root\cimv2")
  Set objAllSoftwares = objWMIService.ExecQuery("Select * from Win32_Product")
  </code></pre>
  
  <p>For some reason it does not retrieve all the software installed (which you can see in Add/Remove Programs) e.g. 7-Zip (64 bit), GPL Ghost Script to name a few
  Any help would be much appreciated.</p>
  
  <p>Thanks</p>
  
  <p>Dumidu</p>