Moving .eml Files in Powershell

I have a text file with a list of .eml files I generated and want to move them to another location. Because of the way the file was generated there are some duplicates.

The script looks like it should work. All the Variables are at least showing correct.

$filepath="G:\APPS\temp\Eric\Chris2.txt"

$new = 'G:\APPS\temp\Eric\Rel-emails'

$old = 'C:\a\b'

$OldName = (GC -path $filepath -Raw) | Get-Unique
ForEach ($item in $OldName)  {
       $NewName = $OldName -replace [regex]::Escape("$old"),$new
       Move-item -Path $Item -Destination $NewName -WhatIf
}

Error:

' does not exist.
At line:10 char:8
+        Move-item -Path $Item -Destination "$NewName" -WhatIf
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand

  • 1

    you’re referencing $OldName which by the looks of it, could be a collection.

    – 

  • @Abraham Zinala So I shouldn’t use get-content?

    – 

  • 1

    -Destination is expecting a value of type [string] and what you’re passing it is an array when you assign it to $NewName inside your loop. A change of logic should get you squared away, think you can handle it?

    – 

  • 1

    So $NewName = $Item -replace [regex]::Escape("$old"),$new will work since it’s evaluating each item in the array?

    – 




  • 1

    In theory, yes. Give it a shot and if you have any issues feel free to update the question, otherwise if that works, we can just mark this as a typo

    – 

Leave a Comment