Skip to content

Document the -PreservePathFrom parameter of Copy-Item and Move-Item (PowerShell 7.8) #13403

Open

Description

@soroshsabz

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-Foo cmdlet" 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 ./export

With 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, or PreservePathFromWrongProvider).
  • 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 -FromSession or -ToSession parameters of Copy-Item.
  • -WhatIf previews 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

  1. Add the -PreservePathFrom parameter to the PARAMETERS section of both cmdlet reference pages, noting that it is a dynamic parameter of the FileSystem provider.
  2. Add an EXAMPLE to each page that pipes filtered items from Get-ChildItem while preserving the folder structure (the scenario from the source issue).
  3. Mention in the parameter description that missing destination directories are created automatically, that items outside the base path produce a non-terminating error, that -WhatIf previews 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 .\export

The first command gets all files below the .\logs folder recursively and filters them by their LastWriteTime. The second command copies the remaining files to the .\export folder. Because PreservePathFrom is specified, each file is copied below .\export using its path relative to .\logs; missing subfolders such as .\export\sub1\deep are created automatically.

Related Source Pull Requests

Related Source Issues

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions