title: Powershell - Change metadata from the properties of the file based on the name of the file
body: <p>Ok, so here is my question:</p>
  
  <p>I have a file with a name like this: IMG_20191215_201811.jpg</p>
  
  <p>What i want is to take the part of 201811 which represents hour, and put it in the created date of the file metadata. Maybe the whole thing, with the date with all 20191215_201811.</p>
  
  <p>What is the best approach to do this?</p>
  
  <pre><code>## Q:\Test\2019\05\19\SO_56211626.ps1
  $Directory = "C:\TestFolder"
  
  foreach ($file in (Get-ChildItem -Path $Directory -Filter *.pdf)){
      if($File.BaseName -match '_(\d{4}-\d{2}-\d{2})(_\d)?$'){
          $date_from_file= (Get-Date $Matches[1])
  
          $file.CreationTime   = $date_from_file
          $file.LastAccessTime = $date_from_file
          $file.LastWriteTime  = $date_from_file
          $file | Select-Object Name,CreationTime,LastAccessTime,LastWriteTime
      }
  }
  </code></pre>
  
  <p>Or maybe this? </p>
  
  <pre><code>$Directory = $env:TEMP
  $DateFormat = "yyyy-MM-dd"
  
  # create some test files
  $TestFileList = @(
      'FileA_2017-10-15.pdf'
      'FileB_2016-04-08.pdf'
      'FileC_2018-01-30.pdf'
      'FileD_2019-09-09_1.pdf'
      'FileE_2015-05-05_2.pdf'
      )
  foreach ($TFL_Item in $TestFileList)
      {
      $Null = New-Item -Path $Directory -Name $TFL_Item -ItemType File -Force
      }
  
  $FileList = Get-ChildItem -LiteralPath $Directory -Filter '*.pdf' -File
  foreach ($FL_Item in $FileList) {
  
      # removed split, added regex match to work with ever-growing list of variant file names
      $Null = $FL_Item.BaseName -match '_(?&lt;DateString&gt;\d{4}-\d{2}-\d{2})'
      $DateString = $Matches.DateString
      $date_from_file = [datetime]::ParseExact($DateString, $DateFormat, $Null)
  
      $FL_Item.CreationTime = $date_from_file
      $FL_Item.LastWriteTime = $date_from_file
      $FL_Item.LastAccessTime = $date_from_file
  
      # show the resulting datetime info
      '=' * 20
      $CurrentFileInfo = Get-Item -LiteralPath $FL_Item.FullName
      $CurrentFileInfo.FullName
      $CurrentFileInfo.CreationTime
      $CurrentFileInfo.LastWriteTime
      $CurrentFileInfo.LastAccessTime
      }
  </code></pre>
  
  <p>P.S.: I'm newbie at this stuff so don't jump on me as i don't even know what is what and this kind of stuff is very heavy on my brain, otherwise .... i wouldn't have asked you guys :)</p>
  
  <p>Thanks,
  Bogdan</p>
  
  <p>P.S.2: Kudos to JadonR</p>