Recursively Setting Directory Attributes in PowerShell

As I found out the (excellent) Egnyte Desktop Sync client for Windows ignores directories that have the system attribute set. For some reason some of the directories I wanted to sync did have this attribute set. Getting rid of the system attribute on (many) directories is harder than it seems.

Explorer

While you can get Explorer to display a directory’s attributes…

Explorer showing directory with system attribute set

…it does not let you change all of them. Specifically not the system attribute:

Explorer directory properties

Command Line

The good old attrib command lets you remove the system attribute, of course. It even has an option to work recursively. Unfortunately this applies to files only, not directories:

attrib -s /d /s d:\test1

PowerShell

PowerShell to the rescue! The following command removes the system attribute recursively from all directories, starting at D:\test1:

Get-ChildItem D:\test1 -Recurse -Force -ErrorAction SilentlyContinue | Where-Object {$_.PSIsContainer -and $_.Attributes -match "System"} | foreach {$_.Attributes = ""}

If you only want to list directories with the system attribute use this instead:

Get-ChildItem d:\test1 -Recurse -Force -ErrorAction SilentlyContinue | Where-Object {$_.PSIsContainer -and $_.Attributes -match "System"} | Select-Object FullName

Comments

Related Posts

Creating Realistic Test User Accounts in Active Directory

Creating Realistic Test User Accounts in Active Directory
When you need to simulate a real Active Directory with thousands of users you quickly find that creating realistic test accounts is not trivial. Sure enough, you can whip up a quick PowerShell one-liner that creates any number of accounts, but what if you need real first and last names? Real (existing) addresses? Postal codes matching phone area codes? I could go on. The point is that you need two things: input files with names, addresses etc. And script logic that creates user accounts from that data. This blog post provides both.
Scripting

Latest Posts

Scripted WordPress to Hugo Migration

Scripted WordPress to Hugo Migration
After having published in WordPress for almost 20 years, it was time for a change. This site is now rendered by Hugo, a static website generator built for Markdown content hosted in a Git repository. The migration from WordPress (HTML) to Hugo (Markdown) was far from trivial. Since I couldn’t find any tool for the job, I developed my own set of migration scripts that fully automate the migration process. You can find them on GitHub along with extensive documentation.
Website