PowerTip: Find Files Using the Wrong Year

Doctor Scripto

Summary: Use Windows PowerShell to find files using the wrong year in the file name.

Hey, Scripting Guy! Question I have files that use the month, day, and year in their file names. I recently discovered that some years are not using the new year in their file names. How can I easily find these files?

Hey, Scripting Guy! Answer Use the Get-ChildItem cmdlet to find the files in the folder. Use a wild card pattern in the Where-Object command to filter out these files. The following illustrates this technique by using Windows PowerShell 3.0 syntax, where gci is the alias for Get-ChildItem and ? is the alias for Where-Object:

15:42 C:\> gci -Recurse C:\data\ScriptingGuys\2013 | ? name -like ‘???-?-*-12-*

    Directory: C:\data\ScriptingGuys\2013\HSG_1_7_13

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

-a—         1/10/2013   1:21 PM      20383 HSG-1-11-12-01.png

-a—         1/10/2013   1:24 PM      19952 HSG-1-11-12-02.png

-a—         1/10/2013   1:26 PM      24238 HSG-1-11-12-03.png

-a—         1/10/2013   1:37 PM      42781 HSG-1-11-12-04.png

-a—         1/10/2013   1:37 PM      12472 HSG-1-11-12-05.png

-a—         1/11/2013  12:35 PM      28651 HSG-1-12-12-01.png

-a—         1/11/2013  12:43 PM      38762 HSG-1-12-12-02.png

—–         1/11/2013   1:13 PM      25456 HSG-1-12-12-03.png

-a—         1/11/2013   1:13 PM      16030 HSG-1-12-12-04.png

For Windows PowerShell 1.0 or Windows PowerShell 2.0 syntax, the command is:

gci -Recurse C:\data\ScriptingGuys\2013 | ? {$_.name -like ‘???-?-*-12-*’}



Discussion is closed.

Feedback usabilla icon