Search code examples
powershellcopy-item

Copy Objects from Get-Random


I want to pull random images from a directory, so I use Get-ChildItem with the desired parameters to identify all relevant images, then have them write to a CSV file. From there, I import the CSV and use Get-Random to pull the random files. I then want to copy the images from Get-Random but I cant get it to work.

I know the CSV is correct, and when I call the Get-Random variable, it looks correct (I believe it's a hashtable?).

CSV Contents:

Image                                                                                                              
-----                                                                                                              
\\Path\File1.jpg
\\Path\File2.jpg
\\Path\File3.jpg

Script to pull random image and copy over:

$a = Import-CSV -Path $path
    $selects = Get-Random -InputObject $a -Count $count
        foreach($b in $selects){
        Copy-Item $destination
        }

When I echo the $selects, it does accuratley name random files from the original GCI, but I can't figure out the copy part. Maybe it's because its a hashtable? I believe Get-Random provides objects and I think objects can use Copy-Item. I'm over my head.

Thanks


Solution

  • I was able to circumnavigate the issue by completely removing the CSV element. Initially a Get-Childitem and Add-Content built out the CSV, instead of doing that I have piped the GCI directly into the Get-Random and then used Copy-Item successfully:

    $a = GCI $directory | Get-Random -Count $Count $a | Copy-Item -Destination $Destination

    Thanks for everyone's input, it helped me think outside my current constructs.