Repository navigation
Description
Prerequisites
- Existing Issue: Search the existing issues for this repository. If there is an issue that fits your needs do not file a new one. Subscribe, react, or comment on that issue instead.
- Descriptive Title: Write the title for this issue as a short synopsis. If possible, provide context. For example, "Document new
Get-Foocmdlet" instead of "New cmdlet."
Summary
PowerShell is adding a -PreservePathFrom dynamic parameter (provided by the FileSystem provider) to the Copy-Item and Move-Item cmdlets in PowerShell 7.8. The cmdlet references need to document the new parameter: what it does, how the base path is resolved, the errors it reports, and its interactions with -FromSession/-ToSession, -Recurse, -WhatIf, and pipeline input.
Details
The feature was requested in PowerShell/PowerShell#26161 and is implemented in PowerShell/PowerShell#28467 (targeting master, so the feature ships in PowerShell 7.8 - it is not part of the released PowerShell 7.7).
-PreservePathFrom specifies the base path from which the relative path of each item being copied or moved is preserved at the destination:
Get-ChildItem ./logs -File -Recurse
| Where-Object LastWriteTime -gt ([System.DateTime]::Today.AddDays(-30))
| Copy-Item -PreservePathFrom ./logs -Destination ./exportWith this command, ./logs/sub1/deep/d.log is copied to ./export/sub1/deep/d.log instead of being flattened into ./export/d.log. The same parameter is available on Move-Item.
Behavior worth documenting:
- The parameter is a dynamic parameter of the FileSystem provider, so it is only available when the
Path/LiteralPath(or the piped input) resolves to the FileSystem provider. - The base path must resolve to a single existing directory in the FileSystem provider; otherwise an error is written (
PreservePathFromPathNotFound,PreservePathFromNotDirectory,PreservePathFromAmbiguous, orPreservePathFromWrongProvider). - The relative path of each item below the base path is appended to the destination, and the required destination directories are created automatically. This also works for wildcard paths and for piped directories.
- Items that are not below the base path write a non-terminating error (
PreservePathFromOutsideBasePath) so that pipeline input with mixed items keeps processing. - The parameter cannot be combined with the
-FromSessionor-ToSessionparameters ofCopy-Item. -WhatIfpreviews the automatically created directories and the full destination paths without creating anything.
Articles
The reference updates target the reference/7.8 version folder, which is created upstream when the PowerShell 7.8 preview documentation cycle begins:
- Copy-Item (
reference/7.8/Microsoft.PowerShell.Management/Copy-Item.md) - Move-Item (
reference/7.8/Microsoft.PowerShell.Management/Move-Item.md)
Suggested updates
- Add the
-PreservePathFromparameter to the PARAMETERS section of both cmdlet reference pages, noting that it is a dynamic parameter of the FileSystem provider. - Add an EXAMPLE to each page that pipes filtered items from
Get-ChildItemwhile preserving the folder structure (the scenario from the source issue). - Mention in the parameter description that missing destination directories are created automatically, that items outside the base path produce a non-terminating error, that
-WhatIfpreviews the created directories, and that-FromSession/-ToSession(Copy-Item) cannot be combined with it.
Suggested content
Parameter entry for the PARAMETERS section of each cmdlet:
-PreservePathFrom
Specifies the base path from which the relative path of the item being copied or moved is preserved at the destination. When this parameter is specified, each item is copied or moved below the destination using its path relative to the base path, and missing destination directories are created automatically.
This parameter is a dynamic parameter added by the FileSystem provider. It is only available when the path resolves to a FileSystem drive.
The base path must resolve to a single existing directory. Items whose path is not below the base path produce a non-terminating error.
This parameter cannot be used with the FromSession or ToSession parameters.
Type: String Parameter Sets: (All) Aliases: none Required: False Position: Named Default value: None Accept pipeline input: False Accept wildcard characters: False
Example entry for each cmdlet:
Example: Copy items filtered by date while preserving the folder structure
Get-ChildItem .\logs -File -Recurse | Where-Object LastWriteTime -gt ([System.DateTime]::Today.AddDays(-30)) | Copy-Item -PreservePathFrom .\logs -Destination .\exportThe first command gets all files below the
.\logsfolder recursively and filters them by their LastWriteTime. The second command copies the remaining files to the.\exportfolder. Because PreservePathFrom is specified, each file is copied below.\exportusing its path relative to.\logs; missing subfolders such as.\export\sub1\deepare created automatically.
Activity