Get-Childitem -Recurse does not recursively return

Asked

Viewed 213 times

1

I am trying to create a powershell script to manipulate the files in a directory, but when using the parameter -Recurse I haven’t been getting the sub-folders items

    # onde $source = C:\Files
    # e dentro de Files tem mais duas pastas 001 e 002 com arquivos dentros
    Get-ChildItem -Path $source -Recurse -Filter * |
    ForEach-Object {

        #Recupera a data atual da movimentação
        $date = (Get-Date).ToString();

        #Adiciona conteúdo ao arquivo $log com a data atual da movimentação
        Add-Content -Path $log " $date - O arquivo $_ foi transferido de $source para $target";

        #Escreve em tela
        Write-Host " $date - O arquivo $_ foi transferido de $source para $target";

        #Realiza a copia dos itens
        Move-Item $_.FullName $target;
    }

when I test the command Get-ChildItem -Path $source -Recurse ... in the cmdlet of the powershell it runs normal, but within the ps1 script it does not

  • Try running Ise as administrator ... not sure if it will solve. Ta talking powershell questions here :P

  • i am as administrator already and the script is with rights too

1 answer

1


Get-ChildItem -Path $source -Recurse -Filter * | 
Where-Object { -not $_.PSIsContainer } |
    ForEach-Object {

        # Get the current date
        $date = (Get-Date).ToString();

        # Add to log file
        Add-Content -Path $log " $date - The file $_ was moved from $source to $target";

        # Show on cmdlet
        Write-Host " $date - O arquivo $_ foi transferido de $source para $target";

        # Move items
        Move-Item $_.FullName $target;
    }

The property . Psiscontainer returns true when the item is a container (a directory).

In Powershell v3 and higher can be used as follows:

Get-ChildItem -Path $Source -Directory -Recurse -Filter *

Link to reply on SOEN

Browser other questions tagged

You are not signed in. Login or sign up in order to post.