Extending from my comment. Try something like this.
'PowerShell -Whatif or Confirm'
$Destination = 'D:\temp'
(Get-ChildItem -Directory -Recurse -Exclude '*.pdf' -Depth 3) |
ForEach-Object {
Write-Verbose -Message "Processing: $PSItem" -Verbose
# Remove the whatIf after you validate the results of the move and run it again.
Try {Move-Item -Path $PSItem.FullName -Destination "$Destination\Attachments" -ErrorAction Stop -WhatIf}
Catch
{
Write-Warning -Message "Error processing request"
$PSItem.Exception.Message
}
}
# Results
<#
VERBOSE: Processing: D:\Scripts\.vs
What if: Performing the operation "Move Directory" on target "Item: D:\Scripts\.vs Destination: D:\temp\Attachments".
VERBOSE: Processing: D:\Scripts\.vs\Scripts
What if: Performing the operation "Move Directory" on target "Item: D:\Scripts\.vs\Scripts Destination: D:\temp\Attachments".
VERBOSE: Processing: D:\Scripts\.vs\Scripts\v16
What if: Performing the operation "Move Directory" on target "Item: D:\Scripts\.vs\Scripts\v16 Destination: D:\temp\Attachments".
...
#>
If you want to see what is happening under the covers as part of your check, you can do this.
Trace-Command
# Review what the command expression is doing
Trace-Command -Name metadata,parameterbinding,cmdlet -Expression {
$Destination = 'D:\temp'
(Get-ChildItem -Directory -Recurse -Exclude '*.pdf' -Depth 3) |
ForEach-Object {
Write-Verbose -Message "Processing: $PSItem" -Verbose
# Remove the whatIf after you validate the results of the move and run it again.
Try {Move-Item -Path $PSItem.FullName -Destination "$Destination\Attachments" -ErrorAction Stop -WhatIf}
Catch
{
Write-Warning -Message "Error processing request"
$PSItem.Exception.Message
}
}
} -PSHost