Get-Item $Path | Rename-Item -NewName {
$tokens = $_.Name -split '_' # split the name into tokens
'{0}_{1}' -f $tokens[1], $tokens[0] # output with tokens swapped
} -WhatIf
-WhatIf
previews the operation.
As you can see, you can do the parsing as part of the script block passed to Rename-Item
's -NewName
parameter.
Since -NewName
only expects the item's file or directory new name (as opposed to the full path), $_.Name
is parsed and a transformation of it is (implicitly) output.
Here's a more succinct formulation, inspired by a tip from LotPings:
Get-Item $Path | Rename-Item -NewName { -join ($_.Name -split '(_)')[-1, 1, 0] }
This relies on PowerShell's ability to slice an array by specifying an array (list) of indices: -1, 1, 0
effectively reverses the elements of the array that $_.Name -split '(_)'
returns - note the (...)
around _
, which ensures that instances of _
are included in the array of tokens returned; the unary form of the -join
operator then concatenates the reversed array's elements.
Note: I'm assuming that $Path
contains a wildcard expression that matches only the directories of interest.
If you need to be explicit about matching directories only, use Get-ChildItem
with the
-Directory
switch:
Get-ChildItem $Path -Directory
With a wildcard pattern specifically matching the sample name from your question:
Get-ChildItem [a-z][a-z]_[0-9][0-9][0-9][0-9] -Directory