194 lines
10 KiB
Markdown
194 lines
10 KiB
Markdown
# Prepare known-folder paths for OneDrive
|
||
|
||
`Repair-OneDriveKnownFolderPaths.ps1` scans Documents, Pictures, and Desktop and
|
||
proposes shorter filenames where their names or full paths exceed a length
|
||
budget. Preview is the default. Apply mode renames files **in place**; it does not
|
||
move files, rename folders, configure OneDrive, or enable folder backup.
|
||
|
||
## Preview, review, apply
|
||
|
||
Run in Windows PowerShell 5.1 or PowerShell 7 on Windows. For another user, run
|
||
with access to that user's files and registry hive. Close applications using the
|
||
affected files and pause OneDrive sync while applying renames. Renaming can break
|
||
shortcuts, application references, and links that depend on the original name.
|
||
|
||
```powershell
|
||
$options = @{
|
||
UserProfilePath = 'C:\Users\Jane'
|
||
OneDriveRoot = 'C:\Users\Jane\OneDrive - Contoso'
|
||
Apply = $false
|
||
ReportDirectory = 'C:\ProgramData\Emberkom\Output\OneDrivePathPreparation'
|
||
}
|
||
$preview = .\Repair-OneDriveKnownFolderPaths.ps1 -Options $options
|
||
Import-Csv -LiteralPath $preview.ResultPath |
|
||
Format-Table Status, OriginalPath, NewPath -Wrap
|
||
|
||
# After reviewing the proposed filenames and unresolved rows:
|
||
$options.Apply = $true
|
||
$result = .\Repair-OneDriveKnownFolderPaths.ps1 -Options $options
|
||
```
|
||
|
||
`-WhatIf` also suppresses renames when `Apply` is true; diagnostic reports are
|
||
still written. Each invocation rescans and creates a new plan, so an apply run
|
||
can differ from an earlier preview if the files have changed.
|
||
|
||
When running as the affected user, `UserProfilePath` can be omitted. When only
|
||
one OneDrive account is configured, `OneDriveRoot` can also be omitted. An
|
||
ambiguous or missing account requires an explicit destination. SYSTEM and
|
||
service accounts must identify a target profile; the script never guesses the
|
||
logged-on user or scans every profile.
|
||
|
||
The target profile's loaded `User Shell Folders` registry key resolves redirected
|
||
folders. Its own environment values resolve variables such as `%USERPROFILE%`
|
||
and `%OneDriveCommercial%`. If its hive is not loaded, sign the user in or supply
|
||
all source paths explicitly (no registry hive is loaded by this script):
|
||
|
||
```powershell
|
||
$options.KnownFolderPaths = @{
|
||
Documents = 'D:\UserData\Jane\Documents'
|
||
Pictures = 'D:\UserData\Jane\Pictures'
|
||
Desktop = 'D:\UserData\Jane\Desktop'
|
||
}
|
||
# OneDriveRoot must also be explicit to bypass registry discovery completely.
|
||
```
|
||
|
||
Use actual known folder locations, especially when backup is already partially
|
||
enabled. Paths must be absolute local drive paths. Overlapping source roots,
|
||
network paths, and reports inside the scanned folders or destination are rejected.
|
||
|
||
## Length limits and rename behavior
|
||
|
||
[Microsoft documents](https://support.microsoft.com/en-us/onedrive/what-are-file-path-length-limits)
|
||
a 400-character cloud path limit, 255-character individual names, and a
|
||
520-character combined sync path with a root allowance of 120 characters.
|
||
Windows applications can impose shorter limits.
|
||
|
||
The script defaults to **240 characters for both the current and projected local
|
||
path**, leaving room below common Windows path limits. This is a conservative
|
||
operational default, not OneDrive's cloud limit; it can propose renaming files
|
||
that OneDrive itself would accept. `MaxPathLength` accepts 40–520, and
|
||
`MaxNameLength` accepts 16–255 (default 255). The 400-character path relative to
|
||
the OneDrive root is always enforced independently. These checks do not guarantee
|
||
compatibility with every application's own limits.
|
||
|
||
For folders outside the chosen OneDrive root, the projection is
|
||
`OneDriveRoot\Documents`, `OneDriveRoot\Pictures`, or `OneDriveRoot\Desktop`, plus
|
||
the existing relative path. Folders already beneath that root retain their
|
||
existing relative location. Review this assumption against the intended backup
|
||
layout before applying changes.
|
||
|
||
Only over-budget filenames are shortened. The script retains a readable prefix,
|
||
adds `~` and eight deterministic hash characters, and preserves the final
|
||
extension. For example: `Long proposal title~1a2b3c4d.docx`. Further suffixes resolve
|
||
collisions with existing files, directories, or other proposed names. It never
|
||
overwrites a destination. A filename must have room for at least one prefix
|
||
character, the suffix, and its extension. If it cannot fit, the file is reported
|
||
as `NeedsFolderCleanup` without changing it. A leading-dot file whose entire
|
||
name is treated as an extension may also require manual cleanup.
|
||
|
||
Folder names and nesting are never changed automatically. Long folder paths,
|
||
including empty folders, are reported. Hidden/system entries are included.
|
||
Unicode extended-path Windows APIs allow existing paths longer than 260
|
||
characters to be enumerated and renamed without changing Windows policy.
|
||
Junctions, symlinks, and unknown reparse points are skipped, including their
|
||
subtrees. Modern Cloud Files placeholders are allowed; no file contents are read
|
||
by the preparation script. An unavailable cloud provider or a locked file may
|
||
still prevent enumeration or renaming and will be reported.
|
||
|
||
## Reports and failures
|
||
|
||
Each run writes a unique directory beneath `ReportDirectory` (default:
|
||
`%TEMP%\OneDrivePathPreparation` for the executing account):
|
||
|
||
- `plan.csv`: original paths, proposed paths, projected OneDrive paths, lengths,
|
||
and issues; written before any rename.
|
||
- `results.csv`: the outcome for each proposed rename or issue. Files already
|
||
within budget are omitted, but counted in the summary.
|
||
- `journal.csv`: apply runs record `Pending` before each rename and its outcome
|
||
afterward; each record is flushed to disk. Keep this with the plan/results.
|
||
|
||
Statuses include `Preview`, `Renamed`, `NeedsFolderCleanup`, `InvalidName`,
|
||
`SkippedReparsePoint`, `ScanError`, `NotApplied`, and `RenameFailed`. Invalid-name
|
||
rows flag common non-length restrictions without fixing them. `desktop.ini` and
|
||
Office lock files may be expected exclusions; this is not a complete OneDrive
|
||
readiness audit.
|
||
|
||
Any `ScanError` prevents **all** renames in that run. Other unresolved items are
|
||
reported while fixable filenames can still be renamed. Individual rename
|
||
failures are recorded and the run continues. Report/journal write failures stop
|
||
the run. Existing successful renames are not rolled back automatically. After an
|
||
interruption, a final `Pending` journal row means the rename outcome is uncertain;
|
||
check both paths. Original names in the reports support manual restoration,
|
||
provided the old path is unoccupied. Protect reports as they contain user paths.
|
||
|
||
The returned summary includes `FilesScanned`, `FoldersScanned`, `Previewed`,
|
||
`Renamed`, `ReviewNeeded`, `ScanErrors`, and report paths. `ReviewNeeded` counts
|
||
issue rows, not unique files. RMM runs print these counts and the results path;
|
||
review them even when PowerShell exits normally. After resolving remaining
|
||
issues, resume sync, enable/retry OneDrive folder backup, and verify its status.
|
||
This script does not certify that cloud backup completed.
|
||
|
||
## RMM caller
|
||
|
||
Publish the main script, then upload/paste
|
||
[the caller](../rmm/Repair-OneDriveKnownFolderPaths.ps1) into Syncro as a Windows
|
||
PowerShell 5.1 script with **Run As: System**. As with the Microsoft 365 installer,
|
||
configure individual script variables in the RMM; the caller does not assign or
|
||
overwrite their values. Enter variable names without `$`.
|
||
|
||
| Variable | Value / recommended default |
|
||
| --- | --- |
|
||
| `UserProfilePath` | Target profile, e.g. `C:\Users\Jane`; required for the normal SYSTEM workflow |
|
||
| `OneDriveRoot` | Intended destination, e.g. `C:\Users\Jane\OneDrive - Contoso` |
|
||
| `Apply` | Dropdown: `false`, `true`; default **`false`** (preview) |
|
||
| `MaxPathLength` | `240`; allowed range 40–520 |
|
||
| `MaxNameLength` | `255`; allowed range 16–255 |
|
||
| `ReportDirectory` | `C:\ProgramData\Emberkom\Output\OneDrivePathPreparation` |
|
||
| `DocumentsPath` | Optional source override; default `Auto` |
|
||
| `PicturesPath` | Optional source override; default `Auto` |
|
||
| `DesktopPath` | Optional source override; default `Auto` |
|
||
|
||
All values are strings. Every variable configured in Syncro needs a nonempty
|
||
value. The three source overrides can be omitted from the RMM entirely; if added,
|
||
use `Auto` for all three to discover the user's folders, or provide all three
|
||
absolute paths. Partial overrides fail before scanning. Explicit source paths
|
||
plus `OneDriveRoot` permit scanning without a loaded user registry hive.
|
||
|
||
First run with `Apply=false`, inspect the CSV, then select `true` to apply renames.
|
||
The caller only bootstraps Tools and runs:
|
||
|
||
```powershell
|
||
Run-Script -LivePSScript Repair-OneDriveKnownFolderPaths
|
||
```
|
||
|
||
The core reads the individual shared variables without changing them. Missing
|
||
or blank values use the core defaults: preview, 240/255 length limits, and reports
|
||
under the executing account's temp directory. Missing profile/OneDrive inputs
|
||
follow the discovery and SYSTEM restrictions described above. The RMM's suggested
|
||
report path above takes effect when supplied as `ReportDirectory`.
|
||
|
||
Standalone `-Options` still takes precedence. If it is absent and any of these
|
||
individual variables exist, they form the complete configuration with defaults;
|
||
the legacy `$OneDrivePrepOptions` hashtable is used only when no individual inputs
|
||
exist. This prevents a stale legacy `Apply=true` from enabling changes when a new
|
||
RMM caller omits `Apply`.
|
||
|
||
`Tools.ps1` is unchanged. Apply accepts Boolean values or RMM strings
|
||
`true`, `$true`, `yes`, `y`, `1`; `false`, `$false`, `no`, `n`, `0`, blank, and null
|
||
disable changes. Unknown values fail before scanning. Option-name typos also fail.
|
||
|
||
## Offline verification
|
||
|
||
```powershell
|
||
powershell.exe -NoProfile -File .\tests\Repair-OneDriveKnownFolderPaths.Tests.ps1
|
||
pwsh.exe -NoProfile -File .\tests\Repair-OneDriveKnownFolderPaths.Tests.ps1
|
||
```
|
||
|
||
Tests use synthetic files in a unique scratch directory under the repository,
|
||
then remove that directory. They cover preview/apply, real paths longer than 260
|
||
characters, collisions, preserved contents/extensions, projected destination
|
||
length, Unicode, deep-folder reporting, WhatIf, incomplete scans, repeat runs,
|
||
the rename journal, and RMM shared-scope execution (individual string variables,
|
||
defaults, precedence, and legacy callers) with downloads replaced by local fixtures.
|
||
They do not contact OneDrive or verify cloud synchronization.
|