'Rename file using a hash table

I have the following hash table.

$m = @{
    "AAA" = "XX";
    "BBB" = "YY";
    "CCC" = "ZZ";
    ....
}

I want to rename the files which names started with "AAA" to "XX....", "BBB" to "YY....", etc. For example, "AAA1234.txt" will be renamed to "XX1234.txt".

How to do it in Powershell?



Solution 1:[1]

Great..clearly explained.But this will rename the full folder name.. straight forward.

$m = @{"AA" = "XX"; "BB" = "YY"}
$files = Get-ChildItem -Path C:\test\ -Directory
$m.GetEnumerator() | %{Rename-Item "C:\test\$($_.Key)" -NewName "C:\test\$($_.value)" -Force
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Csala