I want to use the IO.FileSystemWatcher to Crate a Hotfolder. Every File that is thrown into this folder should be printed out on the local standard printer.
I put some extra in logging and sending an notification Mail but the main reason is to print the files and move them in a different folder after they have been printed.
Unregister-Event -SourceIdentifier * $folder = 'C:\Users\R&D\Documents\Python_Powershell_print\printfolder' # Enter the root path you want to monitor. $filter = '*.*' # You can enter a wildcard filter here. # In the following line, you can change 'IncludeSubdirectories to $true if required. $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { $name = $Event.SourceEventArgs.Name $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file '$name' was $changeType at $timeStamp" -fore green Out-File -FilePath C:\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp" start-process -FilePath $name.fullName -Verb Print Move-Item -FilePath "$name.fullName" C:\printed -Verbose -Force *>&1 $MyEmail = "superspamtube@gmail.com" $SMTP= "smtp.gmail.com" $To = "superspamtube@gmail.com" $Subject = "PRINT HOTFOLDER HIT" $Body = "PRINT HOTFOLDER HIT" $Creds = (Get-Credential -Credential "$MyEmail") Start-Sleep 2 Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body -SmtpServer $SMTP -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption never <# $PSEmailServer variable can be used to pre-configure the SMTP server in your Powershell Profile. Then you don't need to specify -smtpserver paramter. Send-MailMessage will use the SMTP sever address assigned to $PSEmailServer Delivery Notification Options: -- None: No notification. -- OnSuccess: Notify if the delivery is successful. -- OnFailure: Notify if the delivery is unsuccessful. -- Delay: Notify if the delivery is delayed. -- Never: Never notify. #> } # To stop the monitoring, run the following commands: # Unregister-Event FileDeleted # Unregister-Event FileCreated # Unregister-Event FileChanged