Powershell- Duplicating Files in same folder Please!

Hello Powershell Noob here, I am trying to write a script that will take paths from my clipboard and just duplicate them right in their parent folder.

Lets say my clipboard looks like this:

"C:\Users\User1\New Folder\Image A.jpg"
"C:\Users\User1\New Folder\Image B.jpg"
"C:\Users\User1\New Folder\Image C.jpg"

I know its just a matter of Copy-Item but I am stuck on correctly appending “(1)” on the file name each file name. I’ve tried:

$objectList = Get-Clipboard
Foreach($Object in $ObjectList){
$NewName = Join-Path -Path $Object -ChildPath ((Split-Path $Object -Leaf) + $i)
Copy-Item -Path $Object -Destination $NewName}

Sometimes I get a result like:

"C:\Users\User1\New Folder\Image A.jpg1"

but I mostly just end up getting red errors, I’ve read the docs and trawled stack overflow and I’ve gotton even more confused.

Can anyone help me understand how to do this? Thanks!

Welcome back to the forum. :wave:t4:

Do you want to rename the files or do you want copy them in the same folder or do you want to move them to the parent folder?

I’d recommend to create proper objects for each individual file with Get-Item or even use Get-ChildItem initially instead of using the strings from the clipboard. This way you can easily access the individual elements of the file. Such as BaseName and Extension.

If you want to copy the files in the same folder it should be enough when you only rename the BaseName. :wink:

And BTW When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.

1 Like

I agree with your suggestion, I will link to other posts next time. Thanks again for helping once more.