need to use powershell to create folders and move files within the local OneDrive folders - I always get errors

Anonymous
2024-02-07T01:50:33+00:00

I have a ps script that moves all images from a staging folder to a set of sub-folders that are created for the pictures in yyyy\yyyy-MM\ naming pattern. This script runs fine in folders that are not in the local OneDrive folder, but fails for any file that is in OneDrive. I've narrowed it down to not being able to create folders or to move files using PowerShell (or Window Command Prompt) when the file is in OneDrive.

Powershell (and command prompt) is able to list the files in the folders, but not create or move them.

If I do it manually in Windows Explorer, there are no errors, I can create folders, move, delete, etc.

This is in Windows 11 23H2, with PSVersion 5.1.22621.2506, logged in to windows with the same microsoft account used for the OneDrive, for a personal account (not business OneDrive)

Examples

In OneDrive, fails:

PS C:\users\xxxx\OneDrive\Documents> new-item 'testDir' -Type Directory
new-item : Could not find file 'testDir'.
At line:1 char:1
+ new-item 'testDir' -Type Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\users\xxxx\...cuments\testDir:String) [New-Item], FileNotFoundException
    + FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

Not in OneDrive, succeeds:

PS C:\users\xxxx\Documents> new-item 'testDir' -Type Directory

    Directory: C:\users\xxxx\Documents

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2/6/2024   5:35 PM                testDir

***moved from Windows / Windows 11 / Files, folders, and storage***
Windows for business Windows Client for IT Pros Storage high availability Clustering and high availability

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question. To protect privacy, user profiles for migrated questions are anonymized.

0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. Anonymous
    2024-02-09T04:40:30+00:00

    Hi 10101 0x3F,

    Try pausing OneDrive sync temporarily while running the PowerShell script. Also specify the full path with the -Path parameter and see if the issue persists.

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-02-10T00:30:20+00:00

    I still have the same issue. I stopped the OneDrive sync using the notification area, and when that didn't work I killed all processes and services named OneDrive or OneSync...

    The powershell command I tried and the error message:

    PS C:\users\xxxx\OneDrive\Documents> new-item -path . -name "testdir" -type Directory
    new-item : Could not find file 'testdir'.
    At line:1 char:1
    + new-item -path . -name "testdir" -type Directory
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\users\xxxx\...cuments\testdir:String) [New-Item], FileNotFoundException
        + FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
    
    0 comments No comments
  3. Anonymous
    2024-02-16T05:36:27+00:00

    What if the full path is specified with the -Path parameter?

    new-item -path "C:\users\xxxx\OneDrive\Documents\testdir" -type Directory
    
    0 comments No comments
  4. Anonymous
    2025-02-14T14:45:45+00:00

    # Define the path to the OneDrive Documents folder

    $OneDrivePath = [Environment]::GetFolderPath('MyDocuments')

    # Define the name of the new folder

    $NewFolderName = "Citavi 6"

    # Combine the paths

    $NewFolderPath = Join-Path -Path $OneDrivePath -ChildPath $NewFolderName

    # Create the new folder

    New-Item -ItemType Directory -Path $NewFolderPath -Force

    Write-Output "New folder created at: $NewFolderPath"

    I have exact the same Problem. The folder not exist

    0 comments No comments