++ Operator on Variable Is Not Changing As Expected In ScriptBlock
问题 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: