PowerShell: How do I append a prefix to all files in a directory?
An alternative approach to a regular expression would be to use simple string operations to create the new file name.
$items=Get-ChildItem;
$items | Rename-Item -NewName { "Prefix_" + $_.Name };
You are quite near.
-replace
uses RegEX and in a Regular Expression you anchor at the beginning with a^
- for a suffix you can similarly replace the anchor at line end with
$
- to avoid any possible re-iteration of the renamed file enclose the first element of the pipeline in parentheses.
(Get-ChildItem -File) | Rename-Item -NewName {$_.Name -replace "^","Prefix_"}