Write-Host "collecting event logs..."
$Date = (get-date) - (new-timespan -day 1)
$PrintEntries = Get-WinEvent -ea SilentlyContinue -ComputerName localhost -FilterHashTable @{ProviderName="Microsoft-Windows-PrintService"; StartTime=$Date; ID=307}
$strOutput = ""
$File = "c:\temp\Printing Audit" + (Get-Date).ToString("yyyy-MM-dd") + ".csv"
write-output "Date;Username;Full Name;Client;Printer Name;Port;Print Size;Pages;Document;SrvComp" | Out-File $File -Encoding UTF8
write-Host "Parsing event log entries..."

ForEach ($PrintEntry in $PrintEntries)
 { 
 #Get date and time of printjob from TimeCreated
 $Date_Time = $PrintEntry.TimeCreated
 #$SrvComp = $PrintEntry.Computer
 $entry = [xml]$PrintEntry.ToXml()
 $SrvComp = $entry.Event.System.Computer
 $docName = $entry.Event.UserData.DocumentPrinted.Param2
 $Username = $entry.Event.UserData.DocumentPrinted.Param3
 $Client = $entry.Event.UserData.DocumentPrinted.Param4
 $PrinterName = $entry.Event.UserData.DocumentPrinted.Param5
 $Port = $entry.Event.UserData.DocumentPrinted.Param6
 $PrintSize = $entry.Event.UserData.DocumentPrinted.Param7
 $PrintPages = $entry.Event.UserData.DocumentPrinted.Param8

 #Get full name from AD
 if ($UserName -gt "")
 {
  $DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher
  $LdapFilter = "(&(objectClass=user)(samAccountName=${UserName}))"
  $DirectorySearcher.Filter = $LdapFilter
  $UserEntry = [adsi]"$($DirectorySearcher.FindOne().Path)"
  $ADName = $UserEntry.displayName
 }
 
 #$rawMessage
 $strOutput = $Date_Time.ToString("yyyy-MM-dd HH:mm:ss")+";" +$UserName+ ";" +$ADName+ ";" +$Client+ ";" +$PrinterName+ ";" +$Port+ ";" +$PrintSize+ ";" +$PrintPages+ ";" +$docName+ ";" +$SrvComp
 write-output $strOutput | Out-File $File -append -Encoding UTF8
}