Script to change the permissions/owner of folders on a file server

Posted on 2009-05-01
Last Modified: 2012-06-27
Due to a long standing practice of laziness when creating user accounts, my company is now left with over 2000 home directories with varying permission levels for various accounts. I've changed the policy on making users, but now I have to clean up the home directory permissions. Being that I've recently started learning (finally) Powershell, I figured I'd use that.

We have all of our home directories in one location, and all are named after the users username, as such:
I figure the easiest way to uniformly change them is to set the permissions of d:\home to the common permissions (Domain Admins: Full, BackupAccount:read, HelpdeskStaff:Change Permissions), then set the permissions on the users individual folders to allow them Change control. Seeing as the directory name always matches the username, I thought it would be fairly easy to use Powershell to grab all the directories, store them in an array, then pass the directory name to ICACLS.exe (I'd use the built in set-acl, but we also want to set the Owner, inheritance, etc... as well). The problem I'm having however, is getting the directory path into a format that works as the username.
I suspect the problem is the .SubString(3) will only work on strings, and I'm trying to use it on an object, but I can't find anything that'll convert the object into a string, or any similair functionality that'll work on an object.

Attached is the part of the script that's giving me fits. Any help appreciated!

$Foldername = Get-ChildItem * | Where-Object {$_.attributes -match "Directory"}

foreach ($Fullpath in $Foldername) {

$username = $Fullpath.SubString(3)

Write-Host $username


Open in new window

Question by:tilbard
    LVL 70

    Accepted Solution


    > then pass the directory name to ICACLS.exe

    If you're using PowerShell you have Get-ACL and Set-ACL available. Given a choice I would use those over ICACLS. It can set the owner and do anything you like with inheritance.

    Anyway, you probably want:

    Get-ChildItem * | Where-Object {$_.attributes -match "Directory"} | %{
      $Username = $_.Name
      $FullPath = $_.FullName
      Write-Host $Username
      Write-Host $FullPath

    The write-host statements are just to show you the values, wasn't quite sure which you wanted from the above.

    LVL 1

    Author Closing Comment

    Perfect, exactly what I needed. Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Why Use PowerShell? Recently, I was asked to come up with a quick way to find out what versions all of our SQL servers were currently running. Well, we have several thousand so taking a manual approach simply wasn't an option. We also didn't have t…
    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now