question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Move-Item: Move directory to another drive fails

See original GitHub issue

Steps to reproduce

Move-Item -LiteralPath 'D:\work\testdir' -Destination 'I:\work\testdir'

D: and I: are just two different simple volume on two different HDD, not DFS or something.

Expected behavior

Directory testdir is moved from D: to I: without error, which is the case with PowerShell-7.2.0-preview.3.

PS D:\tools\PowerShell-7.2.0-preview.3-win-x64> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.0-preview.3
PSEdition                      Core
GitCommitId                    7.2.0-preview.3
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS D:\tools\PowerShell-7.2.0-preview.3-win-x64> Get-ChildItem -LiteralPath 'D:\work'

    Directory: D:\work

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2021/03/20    21:47                testdir

PS D:\tools\PowerShell-7.2.0-preview.3-win-x64> Get-ChildItem -LiteralPath 'I:\work'
PS D:\tools\PowerShell-7.2.0-preview.3-win-x64> Move-Item -LiteralPath 'D:\work\testdir' -Destination 'I:\work\testdir'
PS D:\tools\PowerShell-7.2.0-preview.3-win-x64> Get-ChildItem -LiteralPath 'D:\work'
PS D:\tools\PowerShell-7.2.0-preview.3-win-x64> Get-ChildItem -LiteralPath 'I:\work'

    Directory: I:\work

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2021/03/20    21:59                testdir

PS D:\tools\PowerShell-7.2.0-preview.3-win-x64>

Actual behavior

In PowerShell-7.2.0-preview.4, Move-Item returns error.

PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.0-preview.4
PSEdition                      Core
GitCommitId                    7.2.0-preview.4
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> Get-ChildItem -LiteralPath 'D:\work'

    Directory: D:\work

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2021/03/20    21:59                testdir

PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> Get-ChildItem -LiteralPath 'I:\work'
PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> Move-Item -LiteralPath 'D:\work\testdir' -Destination 'I:\work\testdir'
Move-Item: Source and destination path must have identical roots. Move will not work across volumes.
PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> Get-ChildItem -LiteralPath 'D:\work'

    Directory: D:\work

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2021/03/20    21:59                testdir

PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> Get-ChildItem -LiteralPath 'I:\work'
PS D:\tools\PowerShell-7.2.0-preview.4-win-x64>

Error details

PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> Get-Error

Exception             :
    Type       : System.IO.IOException
    TargetSite :
        Name          : MoveTo
        DeclaringType : System.IO.DirectoryInfo
        MemberType    : Method
        Module        : System.IO.FileSystem.dll
    StackTrace :
   at System.IO.DirectoryInfo.MoveTo(String destDirName)
   at Microsoft.PowerShell.Commands.FileSystemProvider.MoveDirectoryInfoUnchecked(DirectoryInfo directory, String destinationPath, Boolean force)
   at Microsoft.PowerShell.Commands.FileSystemProvider.MoveDirectoryInfoItem(DirectoryInfo directory, String destination, Boolean force)
    Message    : Source and destination path must have identical roots. Move will not work across volumes.
    Source     : System.IO.FileSystem
    HResult    : -2146232800
TargetObject          : D:\work\testdir
CategoryInfo          : WriteError: (D:\work\testdir:DirectoryInfo) [Move-Item], IOException
FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
InvocationInfo        :
    MyCommand        : Move-Item
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 4
    Line             : Move-Item -LiteralPath 'D:\work\testdir' -Destination 'I:\work\testdir'
    PositionMessage  : At line:1 char:1
                       + Move-Item -LiteralPath 'D:\work\testdir' -Destination 'I:\work\testdi …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Move-Item
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

In my quick analysis, #14913 may affect this issue. DirectoryInfo.MoveTo returns IOException with HResult = -2146232800(0x80131620), not -2147024891(0x80070005), which fallback condition for CopyAndDelete expects.

Environment data

PS D:\tools\PowerShell-7.2.0-preview.4-win-x64> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.0-preview.4
PSEdition                      Core
GitCommitId                    7.2.0-preview.4
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveL-MSFTcommented, Mar 21, 2021

Relying on any IOException to try CopyAndDelete() is a big behavioral change and may introduce other subtle side effects. Looking at my original PR, it seems that the mistake was removing the existing code to check the root drive on Windows. The safest fix is to add that check back restoring the previous behavior.

1reaction
jborean93commented, Mar 20, 2021

Is this even possible? Move a directory from one partition to another without copying/deleting it.

No but the code is meant to have a fallback to do the copy and delete if the move fails due to it being in different volumes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Move-Item throws error when attempting to move directory ...
I have a folder containing files, folders, and a target subfolder. I'm trying to move all contents of the folder into the target...
Read more >
Move-Item (Microsoft.PowerShell.Management)
The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another location. The locations must be ......
Read more >
PowerShell Move-Item Folder and Sub Folders Not Always ...
I have a PowerShell script that connects to an SFTP server that downloads files locally in the same folder structure as they are...
Read more >
PowerShell Move-Item examples for file, folder management
More specifically, you can use the Move-Item cmdlet to relocate items based on different criteria. You can move files, while maintaining the ...
Read more >
Move-Item script failed with missing files - powershell
I have a script below that is intended to move zip files from the source directory to target (both are network shares).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found