2

I've successfully created a FileSystemWatcher (C# object). I run the code below in a powershell session.

# Filters
$filter = "somefile.txt"
$flagfolder = "C:\path\to\some\folder"

# Instantiate Watcher 
$Watcher = New-Object IO.FileSystemWatcher $flagfolder, $filter -Property @{ 
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

# EVENT: $filter is created
$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
   $path = $Event.SourceEventArgs.FullPath
   $name = $Event.SourceEventArgs.Name
   $changeType = $Event.SourceEventArgs.ChangeType
   $timeStamp = $Event.TimeGenerated
   Write-Host "The file '$name' was $changeType at $timeStamp"
   Write-Host $path
   someglobalfunction $param
}

After running the code above, if I do a Get-Job it reports the FileWatcher:

Id     Name            PSJobTypeName   State         HasMoreData     Location  
--     ----            -------------   -----         -----------     --------  
74     FileCreated                     NotStarted    False  

But, if I open a new powershell session and do a Get-Job it reports nothing....

I need this to fire whenever anybody or anything creates the file $pathfolder\somefile.txt... but currently it only works when the session that defines $watcher creates the file.

3
  • Jobs don't work like you think they do. Try the code in this answer. Commented Jul 25, 2017 at 21:32
  • "The Get-Job cmdlet gets objects that represent the background jobs that were started in the current session." Emphasis added. Source Also, you assign $flag a value, but never use $flag. Commented Jul 26, 2017 at 15:18
  • @MikeSherrill'CatRecall' sorry, I meant $filter. So I take it this is the wrong way to watch a file? Commented Jul 26, 2017 at 16:02

1 Answer 1

1

You need two things to make this work.

  1. New-Object System.IO.FileSystemWatcher
  2. Register-ObjectEvent

I think this is the minimum code you need to make FileSystemWatcher work.

Set up the FileSystemWatcher object

$w = New-Object System.IO.FileSystemWatcher
$w.Path = $PWD

$PWD (current working directory) is an automatic variable.

Subscribe to the "Created" event (for simplicity)

Register-ObjectEvent -InputObject $w -EventName Created -SourceIdentifier "File.Created" `
    -Action {Write-Host -Object "A file was created" -ForegroundColor Green -BackgroundColor Black}

Now, if you create a file in the current working directory, PowerShell will write "A file was created" to the screen. It doesn't matter how that file gets created--in the current session, in a different PowerShell session, through File Explorer, using cmd.exe--doesn't matter.

This answer has more elaborate code. But the simplified code here is based directly on it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.