0

I am trying to run powershell script to separate files of all extensions by iterating all the subfolders and creating a subfolder attachments at depth 3 except pdf but it is not working. Can someone help me out by pointing what I am doing incorrectly in script. Thanks in advance

ForEach($Folder in (Get-ChildItem -Directory .\*\*\*)){
    echo "Done"
    Get-ChildItem -path $Folder -Exclude *.pdf  | Move-Item -Destination $Folder\Attachments -ErrorAction Stop
}

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48

2 Answers2

0

I'd suggest adding the -WhatIf parameter to Move-Item to troubleshoot what that command is actually trying to do. It will probably be quite clear from the output that it's not doing what you think it's doing.

My guess is the problem is the fact that $Folder contains a DirectoryInfo item. The default string expansion for that is just the name, and you probably want the FullName.

Try:

ForEach($Folder in (Get-ChildItem -Directory .\*\*\*)){
    echo "Done"
    Get-ChildItem -path $Folder -Exclude *.pdf  | Move-Item -Destination "$($Folder.FullName)\Attachments" -ErrorAction Stop
}

However, it's not clear from your question what you're actually trying to accomplish, primarily because "it doesn't work" is not a problem description. I'm not sure where the attachments folder is supposed to be.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • Stringifying to `.Name` rather than `.FullName` is generally limited to _Windows PowerShell_ (as opposed to PowerShell [Core] v6+) and there it only applies _situationally_, and with the specific command at hand it does _not_ apply. For the (hopefully) exact rules on when it does apply, see [this answer](https://stackoverflow.com/a/53400031/45375). – mklement0 Dec 19 '20 at 22:54
  • I tried adding -WhatIf parameter and it shows as moving files but files are not moved into attachments folder What if: Performing the operation "Move File" on target "Item: C:\new_folder\2020\Febraury\TC-AK2- 000001\16760_PastedFromClipboard.png Destination: C:\new_folder\2020\Febraury\TC-AK2-000001\Attachments" – quicklearner Dec 20 '20 at 06:32
  • 2
    Whatif and Confirm are switches that allow you to check your results before they actually happen. Remove the -WahtIf and Confirm, after you look at your results, and run it again to have it complete. "Never, ever run destructive code (New, delete, move, update, modify, etc.) until you check it first." If you do not, you can seriously damage your system, data, and or your entire enterprise. Especially if you do not have/use/validate a mandatory backup/disaster recovery strategy. The message you are seeing is the correct/default response when using WhatIf or Confirm. See the help files. – postanote Dec 20 '20 at 08:25
0

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
postanote
  • 15,138
  • 2
  • 14
  • 25