title: Powershell Log file
body: <p>Firstly I want to point out that I'm a PowerShell ScrapBooker and not really terribly knowledgeable about PowerShell.</p>
  
  <p>I've been working on a script that installs BGInfo ... I have the actual install and uninstall working perfectly, and now I'm moving on to getting the logging sorted out nicely.</p>
  
  <p>I found this article "<a href="https://stackoverflow.com/questions/7834656/create-log-file-in-powershell">Create Log File in Powershell</a>" which was wonderful and have incorporated this Function into my script.</p>
  
  <pre><code>Function Write-Log {
  [CmdletBinding()]
  Param(
  [Parameter(Mandatory=$False)]
  [ValidateSet("LABEL","INFO","WARN","ERROR","FATAL","DEBUG")]
  [String]
  $Level = "INFO",
  
  [Parameter(Mandatory=$True)]
  [string]
  $Message,
  
  [Parameter(Mandatory=$False)]
  [string]
  $logfile
  )
  
  $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
  IF ($Level -eq "LABEL") {
      $Line = "$Message"
      }
  ELSE {
      $Line = "$Stamp $Level $Message"
      }
  If($logfile) {
      Add-Content $logfile -Value $Line
  }
  Else {
      Write-Output $Line
  }
  }
  </code></pre>
  
  <p>What I need to know is how to use this to log output from commands.</p>
  
  <p>For Example:</p>
  
  <p>In My script I have this command:</p>
  
  <pre><code>New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo -Value """$InstPath\Bginfo.exe"" $InstPath\$BGTemplateFile $InstOptions" -PropertyType 'String' -Force
  </code></pre>
  
  <p>or this one:</p>
  
  <pre><code>Copy $SourcePath\Bginfo.exe $InstPath
  </code></pre>
  
  <p>What I would like to know is how I can use my function to capture ANY output from that command and log it in my log file.</p>
  
  <p>I guess that I would also like to use this information and apply it to any other commands where I want to log something.</p>
  
  <p>Hopefully that's all clear and makes sense and that someone can help me out.</p>
  
  <p>Cheers,</p>
  
  <p>Dave.</p>
  
  <p>:)</p>