Powershell - Move folder filtered on subfolders

Kelley, Ryan 96 Reputation points
2021-10-14T16:22:56.953+00:00

What I have so far:

Get-ChildItem -Path "\Networkdrive\Folder1\PROD- TEST\Successful" -Recurse |
Where-Object name -match '\w{10}_\w{3}' |
Move-Item -destination "\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful" -Verbose

Inside of : "\Networkdrive\Folder1\PROD- TEST\Successful" are folders that look like: 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 and then in them are more folders that have criteria 10 numbers with an _ and then followed by 3 numbers .

I want to move the: 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 folder if any subfolder contains the criteria to: "\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful", but I want to leave the Structure and files alone.

Right now the code just grabs the folders inside and then moves them, but leaves the "9ce8eab8-cefe-4ec9-9158-ff40612d5c47" folders alone. I want 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 to be moved too.

Thank you for any help! Sorry if this question is confusing.

Windows for business Windows Server User experience PowerShell
Community Center Not monitored
{count} votes

Accepted answer
  1. Kelley, Ryan 96 Reputation points
    2021-10-14T20:39:48.927+00:00

    In case anyone needs this:
    This was the solution:

    $sourcePath = '\Networkdrive\Folder1\PROD- TEST\Successful'
    $destination = '\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful'
    $folderPattern = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
    $subdirPattern = '\d{10}\d{3}' # if the name must match FULLY, use anchors: '^\d{10}\d{3}$'

    Get-ChildItem -Path $sourcePath -Directory | Where-Object {$.Name -match $folderPattern} | ForEach-Object {
    # capture the folders FullName for when we hit the catch block in case of error
    $targetFolder = $
    .FullName
    if (@(Get-ChildItem -Path $targetFolder -Directory | Where-Object { $.Name -match $subdirPattern }).Count) {
    # one or more subfolders found with name matching the $subdirPattern, so move the folder
    Write-Host "Moving folder '$targetFolder'"
    try {
    Move-Item -Path $targetFolder -Destination $destination -Force -ErrorAction Stop
    }
    catch {
    Write-Warning "Error moving folder '$targetFolder':rn$($
    .Exception.Message)"
    }
    }
    }

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2021-10-15T14:06:50.07+00:00

    Hi there,

    If you are planning to get more into PowerShell, this might help you out. PowerShell is a modern command shell that includes the best features of other popular shells. Unlike most shells that only accept and return text, PowerShell accepts and returns .NET objects.

    https://learn.microsoft.com/en-us/powershell/scripting/overview?view=powershell-7.1

    ----------------------------------------------------------------------------------------------------------

    If the reply is helpful, please Upvote and Accept it as an answer

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.