Copy parent folder if it contains certain filetypes RRS feed

  • Question

  • hello everyone, im using powershell for the first time and i think what im trying to do is right judging from tutorials but it dosnt execute (yes ive allowed scripts to execute). im trying to seperate folders that contain a MKV videos from other folders that dont have MKV files in them, just pictures.


    $destination='H:\Holiday Movies'

    if($location=Get-ChildItem $source -Name *.mp4 -File -Recurse)



    Move-Item $source -Destination $destination -Recurse -Force


    can anyone explain why this script wont execute? if i replace Move-Item with Copy-Item it works fine, but i want the files to be move so i dont have two copys.


    Saturday, September 21, 2019 12:55 AM

All replies

  • Please format your code as code: How to use code feature technet forum post.

    Your condition is probably not what you expect ... basically it is "if it's possible to create the variable - do this". It will always be possible to create a variable - even if it's empty. ;-)

    Tis might work better if I got you right:

    $source = 'H:\Holiday'
    $destination = 'H:\Holiday Movies'
    Get-ChildItem -Path $source -Directory | 
        ForEach-Object {
            if((Get-ChildItem $_.FullName -Filter *.mp4 -File -Recurse).count -gt 0){
                Move-Item -Path $_.FullName -Destination $destination
    That will move only folders containing at least one *mp4 file.

    Live long and prosper!


    • Edited by BOfH-666 Saturday, September 21, 2019 1:22 AM
    Saturday, September 21, 2019 1:21 AM