Moving, renaming with range

Hello everyone,

just started a few weeks ago with powershell so im pretty unknown to how everything works.

last week i was busy with the following script.

the scenario: i would like to copy certain file types from a folder and its subfolder to a created folder with todays date. that part i got that working :slight_smile: . for the next step there are 2 options 1: keep the duplicate file’s name and add -copy to it or 2: move all duplicate files to a folder withing the just created folder with todays date. it doenst really matter wich solution. after that i want a search range for example: in the directory C:…\folder\ are more folder named user1, user 2, user 3 etc etc. i want to have a range that the script only searches in user1 til user 5, or only in user 20 til user 40.

i tried and tried and smashed my laptop a few times any help i really apreciated off course :slight_smile:

this is what i got

$destination  = "location to save"

$source = Get-ChildItem -Path "files where to look" -Filter "*.txt" -Recurse

$folderName = (Get-Date).tostring("dd-MM-yyyy")            
New-Item -itemType Directory -Path $destination -Name $FolderName

foreach ($file in $source) {

    Copy-Item -Path $file.FullName -Destination $destination$folderName

EDIT: removed $file_path

Welcome to the forums.

What’s your actual question. The code looks like it should work actually. Except of - you’re creating a variable $file_path twice but you never use it.

You may use VSCode for developing your scripts. It would give you hints according to those errors.

Hi olaf,

thank for your reply
about the $filepath
i got this code (partially) from the internet and when it worked (copy all txt from a folder including subfolders) i was happy and i didnt touch it cause: it worked.

thanks for your reply in the end i want 3 things, but for now, when everything is copied i want that duplicate filenames to be moved in a newly created folder named duplicate. so my question is that you or anyone else can edit my code above so that it works.

i hope it is clear now for this point.