Search code examples
powershellcopy-item

PowerShell : Copy-Item unique files from directory


I have files in my directory : Fa.txt, Fb.txt, Fc.txt, Fd.txt, Fd.lck.

I want to copy unique file Fa.txt, Fb.txt and Fc.txt into a other directory

Here is my command :

Get-ChildItem c:\temp -Filter F*.* | Group-Object {$_.BaseName} | Where-Object{ 
$_.Count -lt 2} | Select-Object Name | ForEach-Object { Copy-Item c:\temp\$_ 
c:\temp\old}

I receive this error :

Copy-Item : Cannot find path C:\temp\@{Name=Fa} because it does not exist.

Of course I see it's missing extension .txt. I tried with c:\temp\$_.txt. I receive this error Copy-Item :

Cannot find path C:\temp\@{Name=Fc}.txt, because it does not exist.

Thanks for your help.


Solution

  • A quick fix:

    Get-ChildItem c:\temp -Filter F*.* | Group-Object {$_.BaseName} | Where-Object{ 
        $_.Count -lt 2} | Select-Object -ExpandProperty Name | ForEach-Object { 
        Copy-Item c:\temp\$_ c:\temp\old}