When using Move-Item cmdlet in PowerShell (7.2.6) on Windows 10, the file will be 're-cased' from its existing case, to whatever casing is used in the -Path parameter used to identify it. As windows is case-insenstive in its filesystem we'd expect a match on the path, but I didn't expect the moved file to be re-cased to that used to find it (rather than the on-disk original casing of the file).
Is this a bug or a feature? I can't find anything that mentions this behaviour in the documentation
For example:
<# 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() # deliberaly 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 }
}
You'll get output of:
FullName
--------
C:\Users\You\test.txt
C:\Users\You\subfolder\TEST.TXT
As you can see, the moved file now has a casing which matches what was used to specify the original path, not the casing of the original file. I'd have thought that as the name implies, the file should be moved, not 're-case'. Yes, on a Windows system this is functionally the same file as Windows is not case sensitive, but I'd have thought that PowerShell 7.2.6 being cross platform, that this would be unwanted behaviour on Windows.
This caused me some minor issues when packaging an application used on a linux based system which was expecting a different file casing (the file was re-cased because of this behaviour).
Is this a bug or a feature?