Guide to editing file metadata using PowerShell

Common metadata

Creation and modification date

  • Single file:

    (Get-ChildItem .\file.ext).CreationTime = "2021-10-22 00:42"
    (Get-ChildItem .\file.ext).LastWriteTime = "2021-10-22 00:42"
    
  • All files in current folder:

    Get-ChildItem | ForEach-Object { $_.CreationTime = "2021-10-22" }
    Get-ChildItem | ForEach-Object { $_.LastWriteTime = "2021-10-22" }
    

Media files

Loading TagLib#

TagLib# is a really great library crafted for this job. Download the NuGet package and unzip it. Copy the TagLibSharp.dll into where you'll be running the script.

.
├── lib
│   ├── net45
│   │   ├── ...
│   └── netstandard2.0
│       ├── TagLibSharp.dll <-- we need this one
│       ├── TagLibSharp.pdb
│       └── TaglibSharp.xml
├── ...
└── ...

Load the DLL:

[System.Reflection.Assembly]::LoadFrom((Resolve-Path "TagLibSharp.dll"))

Image metadata

Open the file and set properties in .ImageTag object and save it.

$photo = [TagLib.File]::Create((Resolve-Path "photo.jpg"))

$photo.ImageTag.Title = "hello"
$photo.ImageTag.Keywords = ("tag", "another tag")
$photo.ImageTag.Comment = "my comment"
$photo.ImageTag.DateTime = [System.DateTime]::Parse("2020-01-02")

$photo.Save()

Video metadata

TagLibSharp can modify video metadata too.

$video = $video = [TagLib.File]::Create((Resolve-Path ./video.mkv))

$video.Tag.Title = "my video"
$video.Tag.Year = 2020
$video.Tag.Comment = "a comment"
$video.Tag.Publisher = "a publisher"
$video.Tag.Description = "a description"

$video.Save()

Cover photo

Load the image file and override .Tag.Pictures with an array.

$cover = [TagLib.Picture]::CreateFromPath((Resolve-Path cover.png))

$video = [TagLib.File]::Create((Resolve-Path video.mp4))
$video.Tag.Pictures = ($cover)
$video.Save()

$music = [TagLib.File]::Create((Resolve-Path music.mp3))
$music.Tag.Pictures = ($cover)
$music.Save()
Last updated: