I have an object with spaces in their properties names. I want to Select-Object @{n='NewName';e={$_.'Old Name'}}
for every NoteProperty. Since there is a lot of them, I created this function. Running this code will return an array of hash tables but I can't get the old name $col to be replaced with the actual old name. I think it's because it's bound to a new execution context but I can't make it to work.
function Rename-Columns {
# Array of HashTable splat to rename columns in Select-Object (TSQL SELECT [col with space] as colwithspace)
param (
[string[]]$Columns,
[hashtable]$RenamePattern = @{Replace='\s|\:|\.|\+|\|';With=''} # remove unwanted cars
)
$return = @()
foreach ($col in $Columns) {
$newName = $col -replace $RenamePattern['Replace'], $RenamePattern['With']
$return += @{n="$newName";e={$_.'"$col"'}} # <- can't replace $col
}
$return
}