Override "ToString"

Hi !

I try to create un program that put files in a specific folder and rename them according to their date.
For exemple, in a first folder I have these files :

  • Valid_test1.pdf (created 05/07/2022)
  • Valid_test2.pdf (created 05/08/2022)
  • In_court_test3.pdf (created 05/02/2022)

I would like to put only valid test in a second folder and rename them. So I would like to have in a second folder :

  • 05072022.pdf
  • 05082022.pdf

There is this command :

(Get-ChildItem -Path C:\Users\UserName\Desktop\Test\Valid_test1.pdf -File -Recurse).LastWriteTime.ToString("ddMMyyyy")

That output :


But I want to make this for all valid test, and when I try this command :

(Get-ChildItem -Path C:\Users\UserName\Desktop\Test -File -Include cod* -Recurse -Force).LastWriteTime.ToString(“ddMMyyyy”)

I have this message error :

Surcharge introuvable pour « ToString » et le nombre d’arguments « 1 ».
Au caractère Ligne:1 : 1

  • (Get-ChildItem -Path C:\Users\margu\Desktop\testp -File -Include cod* …
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Sorry if it’s in french, i don’t found how to put this message in english. I don’t understand why with -Include ToString does not work.

Thank you for your help.

Welcome to the forum. :wave:t4:

If I got it right you want to either copy or move some files and rename them during this process, right? Until now you only showed some code where you display properties from files.

I’m pretty sure we can help you but you should show the complete code. Without that I’d recommend using a loop to iterate over all desired elements.

BTW: When you post code, sample data, console output or error messages please format ALL of it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Thank you Olaf, I will better organize my response.

Here’s the complete script :

$path = 'C:\Users\margu\Desktop\testp\*'
$destination = 'C:\Users\margu\Documents\Programmation\test\'
$file = Get-ChildItem -Path $path -File -Include cod* -R

$name = $file.LastWriteTime.ToString('ddMMyyyy')

foreach ($file in $path)
  Move-Item -Path $file -Destination $name

Merci beaucoup. :wink:

You’re creating a list of files but you’Re not using it in your loop. And you’re setting the name of the destination of the move command outside the loop. If I got it right you can start with something like this:

$path = 'C:\Users\margu\Desktop\testp\*'
$destinationFolder = 'C:\Users\margu\Documents\Programmation\test\'
$fileList = Get-ChildItem -Path $path -Include cod* -File -Recurse

foreach ($file in $fileList) {
    $DestinationFile = Join-Path -Path $destinationFolder -ChildPath ($($file.LastWriteTime.ToString('ddMMyyyy')) + $file.Extension)
    Move-Item -Path $file.FullName -Destination $DestinationFile

With this code all of your files will end up directly in the destination folder. No matter if they have been in a subfolder of the source folder originally. :point_up_2:t4:

Great !

Thank you so much for your help. I improve the script, of course there are errors but I will fix them :

$path = 'C:\Users\margu\Desktop\testp\*'

$destinationFolder = 'C:\Users\margu\Documents\Programmation\test\'

$fileList = Get-ChildItem -Path $path -Include cod* -File -Recurse

$hour10 = (Get-Date 10:00).ToString("HH:mm")

$hour00 = (Get-Date 00:00).ToString("HH:mm")

foreach ($file in $fileList)


  $dateFile = (Get-ChildItem -Path $file -File -Recurse).LastWriteTime

  $hourFile = $dateFile.ToString("HH:mm")

  if ($hourFile -ge $hour10 -and $hourFile -lt $hour00)


    $name = ($file.LastWriteTime.AddDays(1).ToString('ddMMyyyy')) + $file.Extension




    $name = ($file.LastWriteTime.ToString('ddMMyyyy')) + $file.Extension


  $destinationFile = Join-Path -Path $destinationFolder -ChildPath $name

  $dateDestinationFile = (Get-Date (Join-Path -Path $destinationFolder -ChildPath $name)).LastWriteTime


      Move-Item -Path $file.FullName -Destination $DestinationFile -ErrorAction Stop


        if ($dateFile -gt $dateDestinationFile)
            Remove-Item $destinationFile

  while ($false)

It’s intelligent to use Join-Path. The script isn’t finished, for better understand, it will put some files. And these files are published every day. From 10:00 AM, the file communicates informations for next day.