title: How to create a function in Powershell script?
body: <p>How to write a function to avoid Repeated code for my powershell script, which basically display list of application installed on my machine from Add and Remove window present in control panel.</p>
  
  <p>Below is the powershell script </p>
  
  <p>Stack.ps1</p>
  
  <pre><code>Clear-Host
  
  #$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
  $scriptPath = $PSScriptRoot
  $logFilePath= Join-path $scriptPath "TestResults.log"
  
  #If log file exists, then clear its contents 
  if (Test-Path $logFilePath)
  {
      clear-content -Path $logFilePath
  } 
  
  
  $log = "Date Of Testing: {0} " -f (Get-Date)
  $logString = "Process Started." 
  add-content -Path $logFilePath -Value $log -Force 
  add-content -Path $logFilePath -Value $logString -Force 
  add-content -Path $logFilePath -Value "`n" -Force
  
  
  #Validate ADD/Remove Program list
  
  add-content -Path $logFilePath -Value "`n" -Force
  add-content -Path $logFilePath -Value "Add/Remove Programs :" -Force
  add-content -Path $logFilePath -Value "`n" -Force
  
    $InstalledPrograms = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
  
  add-content -Path $logFilePath -Value "`n" -Force
  foreach ($InstalledProgram in $InstalledPrograms )
  {
     if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("Operational Control")))
      {
          $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
          Format-Table –AutoSize
  
          $logString = $InstalledProgram.DisplayName 
          $logp = $InstalledProgram.Publisher
          $logv = $InstalledProgram.DisplayVersion
          $logd= $InstalledProgram.InstallDate
  
          add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
          add-content -Path $logFilePath -Value "Publisher: $logp" -Force  
          add-content -Path $logFilePath -Value "Version: $logv" -Force  
          add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
          add-content -Path $logFilePath -Value "`n" -Force****
  
      }
  
  if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("Management Studio")))
      {
          $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
          Format-Table –AutoSize
  
          $logString = $InstalledProgram.DisplayName 
          $logp = $InstalledProgram.Publisher
          $logv = $InstalledProgram.DisplayVersion
          $logd= $InstalledProgram.InstallDate
  
          add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
          add-content -Path $logFilePath -Value "Publisher: $logp" -Force  
          add-content -Path $logFilePath -Value "Version: $logv" -Force  
          add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
          add-content -Path $logFilePath -Value "`n" -Force
  
      }
  
      if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("System Analyzer")))
      {
          $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
          Format-Table –AutoSize
  
          $logString = $InstalledProgram.DisplayName 
          $logp = $InstalledProgram.Publisher
          $logv = $InstalledProgram.DisplayVersion
          $logd= $InstalledProgram.InstallDate
  
          add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
          add-content -Path $logFilePath -Value "Publisher: $logp" -Force  
          add-content -Path $logFilePath -Value "Version: $logv" -Force  
          add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
          add-content -Path $logFilePath -Value "`n" -Force
  
      }
  
        if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains("STeP")))
      {
          $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
          Format-Table –AutoSize
  
          $logString = $InstalledProgram.DisplayName 
          $logv = $InstalledProgram.DisplayVersion
          $logd= $InstalledProgram.InstallDate
  
          add-content -Path $logFilePath -Value "Product Name: $logString" -Force   
          add-content -Path $logFilePath -Value "Version: $logv" -Force  
          add-content -Path $logFilePath -Value "InstallDate: $logd" -Force  
          add-content -Path $logFilePath -Value "`n" -Force
  
      }   
  }
  </code></pre>
  
  <p>I am talking about the code present under each if Statements that is used to copy the Output to log file. Can i replace that code with function and call the function in each if statement.</p>