I'm trying to rename a large number of plain text files based on the first line. The issue I'm encoutering is that some first lines have invalid characters (illegal characters in path) so I get errors. This is what I'm using:
$files = Get-ChildItem *.txt
$file_map = @()
foreach ($file in $files) {
$file_map += @{
OldName = $file.Fullname
NewName = "{0}.txt" -f $(Get-Content $file.Fullname| select -First 1)
}
}
$file_map | % { Rename-Item -Path $_.OldName -NewName $_.NewName }
Is there a way to ignore special characthers when renaming?
Thanks.