Folders and sub folders copy the text file if matches

Copper Contributor

Hi PowerShell Folks,


Hope you're doing great.

I have a requirement list of folders with devices IP name such, and with each devices IP another folder created based on the month name Jan, Feb, Mar and so on. On each month there is log file is created. 

Now the condition is go and collect previous month log files from each device and last month of the logs to be sent in archive folder in another location.

 please advise how to achieve this.


I managed to get previous month but I dunno how to put it in right commands to get the output.







1 Reply

# Define the TOP-level folder
$TOP_FOLDER = "C:\temp\logs"

# Get all sub folders recursively
$Child_Folders = Get-ChildItem -Path $TOP_FOLDER -Recurse | Where-Object { $_.PSIsContainer -eq $true }
# Create a text file in each sub-folder and add the current date/time as value.
foreach ($foldername in $Child_Folders)
foreach ($Child_Folder in $Child_Folders)

if ($Child_Folders -eq $MonthVal)
Copy-Item -Path "$Child_Folders\*" -Destination "$DestFolder\" -recurse -Force -Verbose }