|
| 1 | +--- |
| 2 | +Module Name: PnP.PowerShell |
| 3 | +schema: 2.0.0 |
| 4 | +applicable: SharePoint Online |
| 5 | +online version: https://pnp.github.io/powershell/cmdlets/Copy-PnPFileMetadata.html |
| 6 | +external help file: PnP.PowerShell.dll-Help.xml |
| 7 | +title: Copy-PnPFileMetadata |
| 8 | +--- |
| 9 | + |
| 10 | +# Copy-PnPFileMetadata |
| 11 | + |
| 12 | +## SYNOPSIS |
| 13 | + |
| 14 | +Synchronizes metadata between files and folders in SharePoint |
| 15 | + |
| 16 | +## SYNTAX |
| 17 | + |
| 18 | +```powershell |
| 19 | +Copy-PnPFileMetadata [-SourceUrl] <String> [-TargetUrl] <String> [-Fields <String[]>] [-Recursive] [-Force] [-Connection <PnPConnection>] [-SourceConnection <PnPConnection>] [-TargetConnection <PnPConnection>] |
| 20 | +``` |
| 21 | + |
| 22 | +## DESCRIPTION |
| 23 | + |
| 24 | +Synchronizes metadata (Created, Modified, Author, Editor) from source files and folders to their corresponding targets without copying the actual content. This cmdlet is useful for restoring lost metadata after migrations where system fields may have been reset. |
| 25 | + |
| 26 | +When updating items, the cmdlet uses `UpdateOverwriteVersion()` to allow setting system fields while avoiding new user-facing versions. |
| 27 | + |
| 28 | +For folders, the cmdlet batches updates per folder to reduce round-trips and improve performance on large libraries. With `-Verbose`, it logs progress for each folder and file processed, including periodic batch flush messages. |
| 29 | + |
| 30 | +Both `-SourceUrl` and `-TargetUrl` can be provided as absolute URLs, server-relative (starting with `/`), or web-relative paths. URLs are normalized against their respective connections: the source URL is normalized using `-SourceConnection` (or the current connection), and the target URL is normalized using `-TargetConnection` (or the current connection). Targets must already exist; if a corresponding target file or folder is not found, it is skipped. |
| 31 | + |
| 32 | +## EXAMPLES |
| 33 | + |
| 34 | +### EXAMPLE 1 (same site, folder, recursive) |
| 35 | + |
| 36 | +```powershell |
| 37 | +Copy-PnPFileMetadata -SourceUrl "Shared Documents/MyProject" -TargetUrl "Shared Documents/MyProject" |
| 38 | +``` |
| 39 | + |
| 40 | +Synchronizes metadata for the MyProject folder and all its contents recursively from the source to the target location, preserving original creation dates, modification dates, and author information. |
| 41 | + |
| 42 | +### EXAMPLE 2 (same site, single file) |
| 43 | + |
| 44 | +```powershell |
| 45 | +Copy-PnPFileMetadata -SourceUrl "Shared Documents/report.docx" -TargetUrl "Shared Documents/report.docx" |
| 46 | +``` |
| 47 | + |
| 48 | +Synchronizes metadata for a single file from the source to the target, restoring the original system fields. |
| 49 | + |
| 50 | +### EXAMPLE 3 (same site, limited fields) |
| 51 | + |
| 52 | +```powershell |
| 53 | +Copy-PnPFileMetadata -SourceUrl "Shared Documents/Projects" -TargetUrl "Shared Documents/Projects" -Fields @("Created", "Modified") -Force |
| 54 | +``` |
| 55 | + |
| 56 | +Synchronizes only the Created and Modified dates for the Projects folder and its contents, without prompting for confirmation. |
| 57 | + |
| 58 | +### EXAMPLE 4 (same site, non-recursive) |
| 59 | + |
| 60 | +```powershell |
| 61 | +Copy-PnPFileMetadata -SourceUrl "Shared Documents/Archives" -TargetUrl "Shared Documents/Archives" -Recursive:$false |
| 62 | +``` |
| 63 | + |
| 64 | +Synchronizes metadata only for the Archives folder itself, without processing its subfolders and files. |
| 65 | + |
| 66 | +### EXAMPLE 5 (cross site, two connections) |
| 67 | + |
| 68 | +```powershell |
| 69 | +$src = Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/archives -ReturnConnection |
| 70 | +$dst = Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/projects -ReturnConnection |
| 71 | +Copy-PnPFileMetadata -SourceUrl "Shared Documents/MyProject" -TargetUrl "Shared Documents/MyProject" -SourceConnection $src -TargetConnection $dst -Verbose |
| 72 | +``` |
| 73 | + |
| 74 | +Synchronizes metadata across two different site connections. |
| 75 | + |
| 76 | +## PARAMETERS |
| 77 | + |
| 78 | +### -Force |
| 79 | + |
| 80 | +If provided, no confirmation will be requested and the action will be performed |
| 81 | + |
| 82 | +```yaml |
| 83 | +Type: SwitchParameter |
| 84 | +Parameter Sets: (All) |
| 85 | + |
| 86 | +Required: False |
| 87 | +Position: Named |
| 88 | +Default value: None |
| 89 | +Accept pipeline input: False |
| 90 | +Accept wildcard characters: False |
| 91 | +``` |
| 92 | +
|
| 93 | +### -Fields |
| 94 | +
|
| 95 | +Specifies which metadata fields to synchronize. Default fields are Created, Modified, Author, and Editor. |
| 96 | +
|
| 97 | +```yaml |
| 98 | +Type: String[] |
| 99 | +Parameter Sets: (All) |
| 100 | + |
| 101 | +Required: False |
| 102 | +Position: Named |
| 103 | +Default value: @("Author", "Editor", "Created", "Modified") |
| 104 | +Accept pipeline input: False |
| 105 | +Accept wildcard characters: False |
| 106 | +``` |
| 107 | +
|
| 108 | +### -Recursive |
| 109 | +
|
| 110 | +If provided, processes folders recursively including all subfolders and files. This is enabled by default. |
| 111 | +
|
| 112 | +```yaml |
| 113 | +Type: SwitchParameter |
| 114 | +Parameter Sets: (All) |
| 115 | + |
| 116 | +Required: False |
| 117 | +Position: Named |
| 118 | +Default value: $true |
| 119 | +Accept pipeline input: False |
| 120 | +Accept wildcard characters: False |
| 121 | +``` |
| 122 | +
|
| 123 | +
|
| 124 | +
|
| 125 | +### -SourceUrl |
| 126 | +
|
| 127 | +Site or server relative URL specifying the file or folder to copy metadata from. Must include the file name if it is a file or the entire path to the folder if it is a folder. |
| 128 | +
|
| 129 | +```yaml |
| 130 | +Type: String |
| 131 | +Parameter Sets: (All) |
| 132 | +Aliases: ServerRelativeUrl |
| 133 | + |
| 134 | +Required: True |
| 135 | +Position: 0 |
| 136 | +Default value: None |
| 137 | +Accept pipeline input: True (ByValue) |
| 138 | +Accept wildcard characters: False |
| 139 | +``` |
| 140 | +
|
| 141 | +### -TargetUrl |
| 142 | +
|
| 143 | +Site or server relative URL specifying the file or folder to copy metadata to. Must include the file name if it is a file or the entire path to the folder if it is a folder. |
| 144 | +
|
| 145 | +```yaml |
| 146 | +Type: String |
| 147 | +Parameter Sets: (All) |
| 148 | +Aliases: TargetServerRelativeUrl |
| 149 | + |
| 150 | +Required: True |
| 151 | +Position: 1 |
| 152 | +Default value: None |
| 153 | +Accept pipeline input: False |
| 154 | +Accept wildcard characters: False |
| 155 | +``` |
| 156 | +
|
| 157 | +### -SourceConnection |
| 158 | +
|
| 159 | +Optional connection to be used for accessing the source file or folder. If not provided, the current connection is used. |
| 160 | +
|
| 161 | +```yaml |
| 162 | +Type: PnPConnection |
| 163 | +Parameter Sets: (All) |
| 164 | + |
| 165 | +Required: False |
| 166 | +Position: Named |
| 167 | +Default value: None |
| 168 | +Accept pipeline input: False |
| 169 | +Accept wildcard characters: False |
| 170 | +``` |
| 171 | +
|
| 172 | +### -TargetConnection |
| 173 | +
|
| 174 | +Optional connection to be used for accessing the target file or folder. If not provided, the current connection is used. |
| 175 | +
|
| 176 | +```yaml |
| 177 | +Type: PnPConnection |
| 178 | +Parameter Sets: (All) |
| 179 | + |
| 180 | +Required: False |
| 181 | +Position: Named |
| 182 | +Default value: None |
| 183 | +Accept pipeline input: False |
| 184 | +Accept wildcard characters: False |
| 185 | +``` |
| 186 | +
|
| 187 | +## RELATED LINKS |
| 188 | +
|
| 189 | +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) |
0 commit comments