Используя ваши примеры, это работает:
$foldernames = 'NA -IncorrectFolderName1',
'N A-1. IncorrectFolderName2',
'N A- 1. IncorrectFolderName3',
'NA-IncorrectFolderName4',
'N A -1.IncorrectFolderName5',
'NA -NA -IncorrectFolderName6',
'NA - NA -IncorrectFolderName7',
'N A - N A - IncorrectFolderName8',
'N A - NA - IncorrectFolderName9',
'RegularFolderName1',
'NA - CorrectFolderName1',
'NA - 1CorrectFolderName2',
'NA - 1. CorrectFolderName3'
$newNames = $foldernames | ForEach-Object { $_ -replace '^(?:(N\s*A\s*-\s*))+(.+)', 'NA - $2' }
$newNames
Результат:
NA - IncorrectFolderName1
NA - 1. IncorrectFolderName2
NA - 1. IncorrectFolderName3
NA - IncorrectFolderName4
NA - 1.IncorrectFolderName5
NA - IncorrectFolderName6
NA - IncorrectFolderName7
NA - IncorrectFolderName8
NA - IncorrectFolderName9
RegularFolderName1
NA - CorrectFolderName1
NA - 1CorrectFolderName2
NA - 1. CorrectFolderName3
Regex details:
(?: Match the regular expression below
( Match the regular expression below and capture its match into backreference number 1
N Match the character “N” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A Match the character “A” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- Match the character “-” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
)+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
. Match any single character that is not a line break character
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)