Powershell Move directoriesbased on name

Here is my suggestion.

I am not exactly sure about your name format and what part you want to extract, so you might have to change the regex accordingly.

Get-ChildItem C:\Scripts\MetadataExport -Directory | foreach {
    # this will look for a 4-digit number in the directory name
    if ($_.Name -match '(?:\b|\D)(\d{4})(?:\b|\D)') {
        $destPath = Join-Path $_.Parent.FullName $Matches[1]
        if (-not (Test-Path -LiteralPath $destPath)) {
            New-Item $destPath -ItemType Directory
        Move-Item -LiteralPath $_.FullName -Destination $destPath

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top