powershellpowershell-3.0powershell-4.0windows-search

How to loop through all video files (kind:video) regardless of what extension those files(could be .mkv / .mp4 / .mov etc.) have?


So I know that by using this command we can loop through all the MKV / MP4 files in the current folder. But I don't want to have to Google and find out what all possible file-types or extensions the genuine Video files could have like .webm, .mkv, .mp4, .mov etc.

Get-ChildItem -Filter *.mkv | Foreach { $_ }

I want to loop through all those regardless of the file-types or extensions, as long their kind is Video file, by that I mean if you remember Windows folder search there's a parameter says kind:video that lists out all videos without getting into the file extension stuff.

How can I do that with Powershell script ??


Solution

  • As for the interactive GUI perspective:Thanks, mclayton.


    In programmatic use:

    You can emulate kind:video - i.e. a request to find all video files irrespective of their format - as follows:

    # Get an array of wildcard patterns that match all filename extensions
    # representing video files, e.g. @('*.3g2', ..., '*.mp4', ...)
    $includePatterns = 
      (
        Get-ItemProperty registry::HKEY_CLASSES_ROOT\.* -ErrorAction Ignore | 
          Where-Object PerceivedType -eq video
      ).PSChildName -replace '^', '*'
    
    # Filter the target directory by those wildcard patterns.
    # Note: If you're targeting the *current* directory, you can simplify to:
    #         Get-Item $includePatterns
    #       With a given path, make sure to append \*
    #       Add -Force to also match hidden files.
    Get-Item .\* -Include $includePatterns  # | ...
    

    Note:


    Online solution, based on web scraping:

    Note:

    $includePatterns = 
      [regex]::Matches(
        (Invoke-RestMethod https://fileinfo.com/filetypes/video), 
        '(?<=<a href="/extension/)[^"]+'
      ).Value -replace '^', '*.'
    
    Get-Item * -Include $includePatterns | Foreach { $_ }
    

    [1] See this answer for background information.