The following outputting old-name/new-name pairs to a TSV file in addition to the copy operation (PSv3+ syntax):
$countRef = [ref] 0
Get-ChildItem -Filter *.pdf -Recurse | ForEach-Object {
$newFullName = '{0}\{1}.pdf' -f $destLoc, ++$countRef.Value
Copy-Item -WhatIf -LiteralPath $_.FullName -Destination $newFullName
[pscustomobject] @{
Old = $_.FullName
New = $newFullName
}
} | Export-Csv -Delimiter "`t" NameMappings.tsv
This creates a TSV (tab-separated values) file with columns named Old and New that contain the old and new full filenames, respectively.
PSv2: The [pscustomobject] @{ ... } syntactic sugar for creating custom objects from hashtables is not available in v2, so New-Object must be used:
$countRef = [ref] 0
Get-ChildItem -Filter *.pdf -Recurse | ForEach-Object {
$newFullName = '{0}\{1}.pdf' -f $destLoc, ++$countRef.Value
Copy-Item -WhatIf -LiteralPath $_.FullName -Destination $newFullName
New-Object PSCustomObject -Property @{
Old = $_.FullName
New = $newFullName
}
} | Export-Csv -Delimiter "`t" NameMappings.tsv
Caveat: -Property accepts a hashtable[1]
, which means that its key ordering is not guaranteed, so the ordering of properties of the resulting object will typically not reflect the input order - in this case it just happens to do so.
If the resulting property order is undesired, you have two options:
Slow, but convenient: insert a Select-Object call with the properties in the desired order (e.g., Select-Object Old, New).
More cumbersome: Construct the object empty at first New-Object PSCustomObject, and then attach properties one by one in the desired order with individual Add-Member calls.
[1] The PSv3+ [pscustomobject] @{ ... } syntax is seemingly also hashtable-based, but it is parsed in a way that preserves the key order; i.e., as if you had implicitly used [ordered] @{ ... }.