• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0

Powershell FileSystemWatcher stops running after script end


+Human.Online    8,609

I have a powershell script that instantiates a FileSystemWatcher to do some file manipulation.  Within the Powershell environment, I run it - it works fine.


If I run from "Right-click, run with Powershell" it runs once and then stops.  The FSW just doesn't persist.


Anyone know a way around this?  I'd like it to continue running (kinda like a daemon)?


Relevant code below:


Write-EventLog -LogName Application -Source “Converter” -EventId 0 -EntryType "Information" -Message "Script started"

$inputFolder = "c:\blahblahblah\files\in";
$inputFiletype = "txt";
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher;
$FileSystemWatcher.Path = $inputFolder;
$FileSystemWatcher.Filter = "*.$inputFiletype"
$FileSystemWatcher.IncludeSubdirectories = $true
$FileSystemWatcher.EnableRaisingEvents = $true;

$EventSubscription = Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action{
    $details = $event.SourceEventArgs
    $FullPath = $details.FullPath
	Write-EventLog -LogName Application -Source “Converter” -EventId 0 -EntryType "Information" -Message "File found: $FullPath"
	Start-Sleep -s 3

	.. Stuff that doesn't matter here ..


To reiterate, when run within the PowerShell ISE - it runs and carries on, the FSW remains active.

When run as a script, it doesn't

Share this post

Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.