I’m new to PowerShell. I would like to add a file path to an ArrayList every time it changes. However, this PowerShell script fails somehow. Any hints what might I be doing wrong?
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Mydir"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$jobs = New-Object System.Collections.ArrayList
$changed = Register-ObjectEvent $watcher "Changed" -Action {
$changedFile = $($eventArgs.FullPath)
$jobs.Add($changedFile)
}
It’s a scope issue. Add the global scope modifier:
see about_Scopes for more help.