Parsing File Name to extract Date RRS feed

  • Question

  • Hi guys,
    Let's say I want to extract the date from the file name "02-01-2016 Test Report.txt"

    How would I use Get-date to accomplish that?



    Tuesday, June 27, 2017 12:49 PM

All replies

  • You wouldn't.  You would use RegEx.

    if('02-01-2016 Test Report.txt' -match '(\d+-\d+-\d+)'){[datetime]$matches[0]}


    • Proposed as answer by BOfH-666 Tuesday, June 27, 2017 1:10 PM
    Tuesday, June 27, 2017 12:59 PM
  • Hello Bart

    What jrv says is true. You cannot use the Get-Date here, because this generates you a date-object and not something from a file name / string. 
    If you want to extract something from a string or test if something exists in a string, use regex. Quick reference for the most useful "commands" will you find here.


    Tuesday, June 27, 2017 1:15 PM