PowerShell: Finding folders that don’t match a pattern

This week I came across an issue where some folders didn’t match a naming convention that was required by another third-party system. Because of this, data wasn’t being extracted from all of the incorrectly-named folders.

The naming convention in question goes like this: “{whatever} {4-digit-number}”. Lots of folders were missing the relevant number at the end.

To quickly identify which folders were wrongly-named, I used a regex that searches the end of a string for a space followed by four numbers. I then used that in conjunction with Where-Object:

Get-ChildItem -Path | Where-Object {$_.Name -notmatch " d{4}$"}

Then, so that I could identify who was naming new folders incorrectly, I then did this:

Get-ChildItem -Path  | Where-Object {$_.Name -notmatch " d{4}$"} | Select-Object -Property Name,CreationTime,@{Name="Owner";Expression={(Get-Owner $_.FullName).Owner.AccountName}}

I then piped the above command to Sort-Object, and exported the lot to a CSV file for the department in question to review:

Get-ChildItem -Path  | Where-Object {$_.Name -notmatch " d{4}$"} | Select-Object -Property Name,CreationTime,@{Name="Owner";Expression={(Get-Owner $_.FullName).Owner.AccountName}}| Sort-Object CreationTime -Descending | Export-Csv c:tempinsolfolder.csv -NoTypeInformation

In all, a quick, easy, and repeatable way of getting a report out to the people who need to maintain the folder structure. It’s possible to extend it further to just email the owners of each of the non-compliant folders on a set schedule.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s