如何使用 PowerShell 从文件名中删除表情符号?

How to remove emojis from a filename using PowerShell?

如何使用 PowerShell 从文件名中查找和删除表情符号? 例如,我想删除像和这样的表情符号。

我已经尝试了下面的代码,但是它不起作用。 PowerShell 似乎无法处理 utf32 编码。

Get-ChildItem -recurse . | where {$_.Name -match "[\u1F600\u1F64F]"}

您可以直接粘贴符号(运行 v5.1);我想如果 Windows 可以解释它,那么 PowerShell 也可以。

Example

您可以使用来自此网站的正则表达式:Emojis in Javascript。我尝试了几种不同的表情,似乎效果很好。

正则表达式字符串:

(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32-\ude3a]|[\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])

表情符号在 powershell 中表示为该范围内的 16 位代理项对。像 0x1F600 这样的代码太高而无法用 powershell 使用的 16 位来表示。表情符号实际上是 2 个字符长。单独它们是不可打印的。 -cmatch 是一种预防措施,因为有几个 unicode 字符在 ascii 范围 İ K 中具有小写版本。无论如何,使用不区分大小写的匹配与 unicode 范围没有任何意义。请注意,使用 non-ascii 个字符编码 'utf8 no bom' 的脚本在 powershell 5 中不起作用。

# U+D800 to U+DBFF (called "high surrogate") gets combined with another 
# Unicode code point from range U+DC00 to U+DFFF (called "low surrogate")

echo hi > file
dir | where name -cmatch '[\uD800-\uDFFF]' | 
  rename-item -newname { $_.name -creplace '[\uD800-\uDFFF]' } -whatif

What if: Performing the operation "Rename File" on target "Item:
 C:\Users\js\foo\file Destination: C:\Users\js\foo\file".

Surrogate Pairs and Variation Selectors