rename-item-cmdlet

batch renaming files using powershell

房东的猫 提交于 2021-02-17 06:47:08
问题 I am able to batch rename files in a working directory by using: Dir | %{Rename-Item $_ -NewName ("0{0}.wav" -f $nr++)} However I want the file rename to start at something other than zero. Say 0500, and rename sequentially in order. Dir | %{Rename-Item $_ -NewName ("0{500}.wav" -f $nr++)} returns error. How can I tell rename to start at a number other than 0? 回答1: You can initialize the counter beforehand to 500. Also, you don't need to use a ForEach-Object loop ( % ) for this, because the

'Rename-Item' makes a copy with the new name

微笑、不失礼 提交于 2021-01-28 06:31:51
问题 Trying to make a script to backup specific folders and then rename the GUID key for the target user under HKLM...\ProfileLists but the rename-item command makes a copy of the key and creates a new key with the appended name though having full access Tried with -force, tried with move-item instead of rename but it gives the exact same results, a new identical key as original but with an appended name if ((Test-Path $FULLPATH)) { Rename-Item $FULLPATH -NewName "$SSID.bak" -Force if ($?) { Write

Powershell, rename-item doesn't work as expected

时光毁灭记忆、已成空白 提交于 2020-01-11 06:44:10
问题 I have a bunch of jpg image files named in the following pattern: 0001-rand01_012.jpg 0002-rand03_034.jpg I want to rename them by removing the first 5 characters to get the form: rand01_012.jpg etc.. I use the following command: Get-ChildItem | Rename-Item -NewName {$_.name.Substring(5)} When using this with -whatif flag i get the expected message saying: Performing the operation "Rename File" on target "Item: C:\Users\xxxx\Documents\xx xx\temp2\0123-rand16_030.jpg Destination: C:\Users\

rename each file with new-guid in powershell

天涯浪子 提交于 2019-12-22 12:45:10
问题 I have been trying to rename each txt file in a folder with the filename $New-Guid .txt, so they should all have unique names. Anyone know the simple way to do this? 回答1: tl;dr Get-ChildItem ... | Rename-Item -NewName { "$(New-Guid).txt" } Note the use of a script block ( { ... } ) and the call to New-Guid inside $(...) embedded in an expandable string ("..."). File renaming / moving operations with multiple input files are most efficiently performed with delay-bind script blocks , where a

++ Operator on Variable Is Not Changing As Expected In ScriptBlock

99封情书 提交于 2019-12-12 08:58:13
问题 I am trying to rename files by putting a prefix based on an incrementing counter in the files such as: $directory = 'C:\Temp' [int] $count=71; gci $directory | sort -Property LastWriteTime | ` rename-item -newname {"{0}_{1}" -f $count++, $_.Name} -whatif Yet all the files processed are 71_ and $count in $count++ never increments and the filenames are prefixed the same? Why? 回答1: The reason you cannot just use $count++ in your script block in order to increment the sequence number directly is:

++ Operator on Variable Is Not Changing As Expected In ScriptBlock

纵饮孤独 提交于 2019-12-04 11:49:20
I am trying to rename files by putting a prefix based on an incrementing counter in the files such as: $directory = 'C:\Temp' [int] $count=71; gci $directory | sort -Property LastWriteTime | ` rename-item -newname {"{0}_{1}" -f $count++, $_.Name} -whatif Yet all the files processed are 71_ and $count in $count++ never increments and the filenames are prefixed the same? Why? The reason you cannot just use $count++ in your script block in order to increment the sequence number directly is: Delay-bind script blocks - such as the one you passed to Rename-Item -NewName - and script blocks in