PowerShell 7.2.6 Move-Item在Windows 10更改文件大小写:Bug还是特性?
Move-Item 修改文件名大小写的行为:Bug还是特性?
在Windows 10系统中使用PowerShell 7.2.6的Move-Item cmdlet时,移动后的文件会采用-Path参数中指定的大小写,而非原文件在磁盘上存储的大小写。Windows文件系统不区分大小写,路径匹配符合预期,但文件大小写被修改的行为未在官方文档中提及。
这种行为在Windows系统中功能上无影响,但PowerShell作为跨平台工具,该行为会对依赖特定大小写的Linux应用造成影响。
示例脚本
<# PowerShell Script to demonstrate how Move-Item changes the casing of a file based on the case used in the Path parameter. #> $base_dir = '~' $original_file_name = 'test.txt' $original_file_path = [System.IO.Path]::Combine($base_dir, $original_file_name) $search_filename = $original_file_path.ToUpper() # deliberately change the case of the file $move_to_path = [System.IO.Path]::Combine($base_dir, 'testsubfolder') # this folder will be removed so ensure it doesn't match something you already have in your home folder that you want to keep $cleanup = $true # set to false to persist files used for testing # Create a file to test Set-Content -Path $original_file_path -Value "hello world" # Setup a subfolder to move the file to if ((Test-Path -Path $move_to_path)) { Remove-Item -Path $move_to_path } New-Item $move_to_path -ItemType Directory -Force | Out-Null # Show the casing of the original file Get-ChildItem -Path ([System.IO.Path]::Combine($base_dir, '*')) -Filter $original_file_name | Select-Object -Property FullName # Deliberately case the name of the file differently, starting with a capital 'T' Move-Item -Path $search_filename -Destination $move_to_path | Out-Null #Show the casing of the 'moved' file Get-ChildItem -Path $move_to_path | Select-Object -Property FullName if ($cleanup) { if (Test-Path -Path $original_file_name -PathType Leaf) { Remove-Item -Path $original_file_name } if (Test-Path -Path $move_to_Path -PathType Container) { Remove-Item -Path $move_to_path } }
执行输出
FullName -------- C:\Users\You\test.txt C:\Users\You\testsubfolder\TEST.TXT
从输出可见,移动后的文件大小写与-Path参数一致,而非原文件的大小写。特此问询:此行为是Bug还是特性?
内容的提问来源于stack exchange,提问作者Jayden
相关产品推荐
相关产品推荐

