1

I have the following in powershell to rename

$version = "2.1.1.1"

But i want to make a copy or rename in the same directory as

myprogram_2.1.1.1.exe

The below doesnt work

Rename-Item -Path "C:\myprogram.exe" -NewName "myprogram.exe" + $version

Any help with this ?

1
  • add to the .BaseName, not to the entire name [including the extension]. [grin] Commented Jul 28, 2020 at 3:13

4 Answers 4

1

Try this:

$version = "2.1.1.1"
$NewName = (Get-Item "C:\myprogram.exe").Basename + "_" + $version + (Get-Item "C:\myprogram.exe").Extension
Rename-Item -Path "C:\myprogram.exe" -NewName "$NewName"
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

Rename-Item -Path "c:\myprogram.exe" -NewName "myprogram${version}.exe" 

When including a variable in a double-quoted string without trailing spaces, you can use ${variable} to enclose it.

Comments

0

You can simply do this:

Rename-Item -Path "C:\myprogram.exe" -NewName "myprogram_$version.exe"

Comments

0

Or:

$version = '2.1.1.1'
dir myprogram.exe | rename-item -newname { $_.BaseName + $version + $_.Extension } -whatif

What if: Performing the operation "Rename File" on target "Item: 
C:\Users\js\myprogram.exe Destination: 
C:\Users\js\myprogram2.1.1.1.exe".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.