DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

PowerShell: List files modified after xx and watch files

This is just a memo for myself.

List files modified after x seconds.

  • Recurse: include subdirectories
  • Force: include hidden files
  • File: file only
Get-ChildItem -Recurse -Force -File | `
where LastWriteTime -gt (Get-Date).AddSeconds(-10) | `
select Name, LastWriteTime
Enter fullscreen mode Exit fullscreen mode

Show tree structure of specified folder.

tree /F <folder>
Enter fullscreen mode Exit fullscreen mode

Watch object creation and update of specified folder.


$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "my files path"
$watcher.EnableRaisingEvents = $true
$watcher.IncludeSubdirectories = $true
$action = {
    $fullPath = $event.SourceEventArgs.FullPath
    $name = $event.SourceEventArgs.Name
    $changeType = $event.SourceEventArgs.ChangeType
    Write-Host "$changeType $name $path"
}
Register-ObjectEvent $watcher 'Changed' -Action $action
#Get-EventSubscriber | Unregister-Event
Enter fullscreen mode Exit fullscreen mode

Top comments (0)