title: Powershell Script to deploy multiple software using variables
body: <p>I am building a deployment script to install software on a new device using a ppkg file.</p>
  
  <p>The script looks at which drive is the USB drive and copies the software over to the local temp folder and runs them according to a set of variables as shown below.</p>
  
  <p>What I am struggling to do is simplify the script so I am not repeating code 7 times down the page, I want to just run a loop 7 times to pull in the needed software. I tried an array but I think I am not quite understanding it completely.</p>
  
  <p>This is my script so far with the repeating code:</p>
  
  <pre><code>#SOE application Variables
  
  #applcation1 CM_client
  $app1name = "Config Manager Client 1706"
  $app1skip = "no"
  $app1path = "$env:SystemDrive\temp\soe\application_installs\app1\CM_client_inst_1706\"
  $app1runcommand = "clientx64.bat"
  $app1arguments = ""
  #applcation2
  $app2name = "Office 2016 Pro Plus"
  $app2skip = "no"
  $app2path = "$env:SystemDrive\temp\soe\application_installs\app2\O2016\"
  $app2runcommand = "setup.exe"
  $app2arguments = "/configure configuration.xml"
  #log Folder
  $datetime = Get-Date -format "yyyy.MM.dd-HH.mm.ss"
  $logpath = "$env:ALLUSERSPROFILE\SOEInst_ppkg\$datetime"
  New-Item -Path $logpath -ItemType Directory -ErrorAction SilentlyContinue
  
  #Transcript Start
  Start-Transcript -Path $logpath\SOE-app-installer-ppkg-$datetime.log
  
  #Timer Function
  $pkgremovetime = Get-Date -format "HH:mm:ss"
  write-host "Script Start Time - $pkgremovetime"
  
  #Find USB Drive
  Write-host Discovering USB Drive
  
  $drives = (GET-WMIOBJECT –query “SELECT * from win32_logicaldisk").DeviceID
  
  foreach ($drive in $drives) {
  
              $usbdrive = (dir $drive USBIMG.FILE | Select-Object -Unique "USBIMG.FILE")
              if ($usbdrive -match "USBIMG.FILE*") {
                      $datadrive = $drive
                      }
              }
  
  Write-host Found $datadrive is the USB drive
  
  #Copy Applications to Local Drive
  Write-Host Creating Installer Folder
  New-Item -Path $env:SystemDrive\temp\SOE -ItemType Directory
  Copy-Item $datadrive\application_installs $env:SystemDrive\temp\soe -Recurse -Verbose
  
  #Install Applications
  #Application 1
      if ($app1skip -eq "no") {
      if ($app1arguments) { #Arguments Variable Populated
          Write-Host Installing Applcation 1 `($app1name`)
          $app1 = Start-Process -Wait -FilePath $app1path$app1runcommand -ErrorAction Continue -ArgumentList $app1arguments -WindowStyle Normal
              if ($app1.ExitCode -eq "0") {
                  Write-Host $app1name Installed ok
                  } Else {
                      Write-host $app1name install exited with code $app1.ExitCode
                      } 
                  }
              }Else { #Argurments Variable Empty
                  Write-Host Installing Applcation 1 `($app1name`)
                  $app1 = Start-Process -Wait -FilePath $app1path$app1runcommand -ErrorAction Continue -WindowStyle Normal
                  if ($app1.ExitCode -eq "0") {
                      Write-Host $app1name Installed ok
                      } Else {
                          Write-host $app1name install exited with code $app1.ExitCode
                  }
              }
  
  
  #Application 2
  if ($app2skip -eq "no") {
      if ($app2arguments) { #Arguments Variable Populated
          Write-Host Installing Applcation 2 `($app2name`)
          $app2 = Start-Process -Wait -FilePath $app2path$app2runcommand -ErrorAction Continue -ArgumentList $app2arguments -WindowStyle Normal
              if ($app2.ExitCode -eq "0") {
                  Write-Host $app2name Installed ok
                  } Else {
                      Write-host $app2name install exited with code $app2.ExitCode
                      } 
                  }
              }Else { #Argurments Variable Empty
                  Write-Host Installing Applcation 2 `($app2name`)
                  $app2 = Start-Process -Wait -FilePath $app2path$app2runcommand -ErrorAction Continue -WindowStyle Normal
                  if ($app2.ExitCode -eq "0") {
                      Write-Host $app2name Installed ok
                      } Else {
                          Write-host $app2name install exited with code $app2.ExitCode
                  }
              }
  #cleanup
  
  Remove-Item $env:SystemDrive\temp\soe -Recurse -Force -Verbose
  
  #get end time
  $pkgremovetime_end = Get-Date -format "HH:mm:ss"
  
  #calculate time difference
  $timetaken = New-TimeSpan $pkgremovetime $pkgremovetime_end
      if ($timetaken.Seconds -lt 0) {
  $Hrs = ($timetaken.Hours) + 23
  $Mins = ($timetaken.Minutes) + 59
  $Secs = ($timetaken.Seconds) + 59 }
      else {
  $Hrs = $timetaken.Hours
  $Mins = $timetaken.Minutes
  $Secs = $timetaken.Seconds }
  $Difference = '{0:00}:{1:00}:{2:00}' -f $Hrs,$Mins,$Secs
  
  #log time difference
  write-host "Script End Time - $pkgremovetime_end"
  Write-Host "Total time taken $difference"
  
  #Transcript End
  Stop-Transcript
  </code></pre>